resource create fails if resource already exists

pull/3477/head
John Calabrese 8 years ago
parent 9486de8733
commit f178c3d670

@ -100,6 +100,12 @@ func (c *Client) Create(namespace string, reader io.Reader, timeout int64, shoul
if buildErr != nil {
return buildErr
}
c.Log("checking %d resources for existing conflicts", len(infos))
if err := c.existingResourceConflict(infos); err != nil {
return err
}
c.Log("creating %d resource(s)", len(infos))
if err := perform(infos, createResource); err != nil {
return err
@ -110,6 +116,26 @@ func (c *Client) Create(namespace string, reader io.Reader, timeout int64, shoul
return nil
}
func (s *Client) existingResourceConflict(infos Result) error {
err := infos.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
helper := resource.NewHelper(info.Client, info.Mapping)
if _, err := helper.Get(info.Namespace, info.Name, info.Export); err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("Could not get information about the resource: %s", err)
}
return fmt.Errorf("Existing resource conflict: %s", info.Name)
})
return err
}
func (c *Client) newBuilder(namespace string, reader io.Reader) *resource.Result {
return c.NewBuilder().
Internal().

@ -355,8 +355,7 @@ func TestCreate(t *testing.T) {
existingPod := strings.NewReader(fmt.Sprintf(testNewPodFormatString, existingResourceName))
err := c.Create("default", existingPod, 3, false)
if err == nil {
t.Skip("This test is pending. still need to implement exit on existing resource.")
t.Errorf("creating a resource which already exists should with proper error, got: %q", err)
t.Errorf("creating a resource which already exists should fail with error, got: %q", err)
}
}

Loading…
Cancel
Save