示例#1
0
func TestUpdateServer_UpdateCredentials(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	update := []api.Update{
		api.Update{
			Op:     "set",
			Member: "password",
			Value: struct {
				Current  string `json:"current"`
				Password string `json:"password"`
			}{
				"current",
				"new",
			},
		},
	}
	client.On("Patch", "http://localhost/v2/servers/test/va1testserver01", update, mock.Anything).Return(nil)
	service := server.New(client)

	name := "va1testserver01"
	resp, err := service.Update(name, server.UpdateCredentials("current", "new"))

	assert.Nil(err)
	assert.Equal("status", resp.Rel)
	client.AssertExpectations(t)
}
示例#2
0
func resourceCLCServerUpdate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*clc.Client)
	id := d.Id()

	var err error
	var edits []api.Update
	var updates []api.Update
	var i int

	poll := make(chan *status.Response, 1)
	d.Partial(true)
	s, err := client.Server.Get(id)
	if err != nil {
		return fmt.Errorf("Failed fetching server: %v - %v", d.Id(), err)
	}
	// edits happen synchronously
	if delta, orig := d.Get("description").(string), s.Description; delta != orig {
		d.SetPartial("description")
		edits = append(edits, server.UpdateDescription(delta))
	}
	if delta, orig := d.Get("group_id").(string), s.GroupID; delta != orig {
		d.SetPartial("group_id")
		edits = append(edits, server.UpdateGroup(delta))
	}
	if len(edits) > 0 {
		err = client.Server.Edit(id, edits...)
		if err != nil {
			return fmt.Errorf("Failed saving edits: %v", err)
		}
	}
	// updates are queue processed
	if d.HasChange("password") {
		d.SetPartial("password")
		creds, _ := client.Server.GetCredentials(id)
		old := creds.Password
		pass := d.Get("password").(string)
		updates = append(updates, server.UpdateCredentials(old, pass))
	}
	if i = d.Get("cpu").(int); i != s.Details.CPU {
		d.SetPartial("cpu")
		updates = append(updates, server.UpdateCPU(i))
	}
	if i = d.Get("memory_mb").(int); i != s.Details.MemoryMB {
		d.SetPartial("memory_mb")
		updates = append(updates, server.UpdateMemory(i/1024)) // takes GB
	}

	if d.HasChange("custom_fields") {
		d.SetPartial("custom_fields")
		fields, err := parseCustomFields(d)
		if err != nil {
			return fmt.Errorf("Failed setting customfields: %v", err)
		}
		updates = append(updates, server.UpdateCustomfields(fields))
	}
	if d.HasChange("additional_disks") {
		d.SetPartial("additional_disks")
		disks, err := parseAdditionalDisks(d)
		if err != nil {
			return fmt.Errorf("Failed parsing disks: %v", err)
		}
		updates = append(updates, server.UpdateAdditionaldisks(disks))
	}

	if len(updates) > 0 {
		resp, err := client.Server.Update(id, updates...)
		if err != nil {
			return fmt.Errorf("Failed saving updates: %v", err)
		}

		err = client.Status.Poll(resp.ID, poll)
		if err != nil {
			return err
		}
		status := <-poll
		if status.Failed() {
			return fmt.Errorf("Update failed")
		}
		log.Printf("[INFO] Server updated! status: %v", status.Status)
	}

	if d.HasChange("power_state") {
		st := d.Get("power_state").(string)
		log.Printf("[DEBUG] POWER: %v => %v", s.Details.Powerstate, st)
		newst := stateFromString(st)
		servers, err := client.Server.PowerState(newst, s.Name)
		if err != nil {
			return fmt.Errorf("Failed setting power state to: %v", newst)
		}
		ok, id := servers[0].GetStatusID()
		if !ok {
			return fmt.Errorf("Failed extracting power state queue status from: %v", servers[0])
		}
		err = client.Status.Poll(id, poll)
		if err != nil {
			return err
		}
		status := <-poll
		if status.Failed() {
			return fmt.Errorf("Update failed")
		}
		log.Printf("[INFO] state updated: %v", status)
	}

	d.Partial(false)
	return resourceCLCServerRead(d, meta)
}