func resourceCLCGroupUpdate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*clc.Client)
	id := d.Id()
	var err error
	var patches []api.Update

	g, err := client.Group.Get(id)
	if err != nil {
		return fmt.Errorf("Failed fetching group: %v - %v", id, err)
	}

	if delta, orig := d.Get("name").(string), g.Name; delta != orig {
		patches = append(patches, group.UpdateName(delta))
	}
	if delta, orig := d.Get("description").(string), g.Description; delta != orig {
		patches = append(patches, group.UpdateDescription(delta))
	}
	newParent := d.Get("parent").(string)
	pgid, err := resolveGroupByNameOrId(newParent, g.Locationid, client)
	log.Printf("[DEBUG] PARENT current:%v new:%v resolved:%v", g.ParentGroupID(), newParent, pgid)
	if pgid == "" {
		return fmt.Errorf("Unable to resolve parent group %v: %v", newParent, err)
	} else if newParent != g.ParentGroupID() {
		patches = append(patches, group.UpdateParentGroupID(pgid))
	}

	if len(patches) == 0 {
		return nil
	}
	err = client.Group.Update(id, patches...)
	if err != nil {
		return fmt.Errorf("Failed updating group %v: %v", id, err)
	}
	return resource.Retry(1*time.Minute, func() *resource.RetryError {
		_, err := client.Group.Get(id)
		if err != nil {
			return resource.RetryableError(err)
		}
		err = resourceCLCGroupRead(d, meta)
		if err != nil {
			return resource.NonRetryableError(err)
		}
		return nil
	})
}
Beispiel #2
0
func TestUpdateGroup(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	client.On("Patch", "http://localhost/v2/groups/test/67890", mock.Anything, mock.Anything).Return(nil)
	service := group.New(client)

	id := "67890"
	patches := make([]api.Update, 3)
	patches[0] = group.UpdateName("foobar")
	patches[1] = group.UpdateDescription("mangled")
	patches[2] = group.UpdateParentGroupID("mangled")

	err := service.Update(id, patches...)

	assert.Nil(err)
	client.AssertExpectations(t)
}