コード例 #1
0
ファイル: server_test.go プロジェクト: ack/clc-sdk
func TestUpdateServer_UpdateGroupAndDescription(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	update := []api.Update{
		api.Update{Op: "set", Member: "groupId", Value: "12345"},
		api.Update{Op: "set", Member: "description", Value: "new"},
	}
	client.On("Patch", "http://localhost/v2/servers/test/va1testserver01", update, mock.Anything).Return(nil)
	service := server.New(client)

	name := "va1testserver01"
	err := service.Edit(name, server.UpdateGroup("12345"), server.UpdateDescription("new"))

	assert.Nil(err)
	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 = make([]api.Update, 0)
	var updates []api.Update = make([]api.Update, 0)
	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 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
	}
	js, _ := json.Marshal(updates)
	LOG.Printf("updates: %v", string(js))
	if len(updates) > 0 {
		resp, err := client.Server.Update(id, updates...)
		if err != nil {
			return fmt.Errorf("Failed saving updates: %v", err)
		}

		client.Status.Poll(resp.ID, poll)
		status := <-poll
		LOG.Printf("Server updated! status: %v", status)
	}

	if d.HasChange("power_state") {
		st := d.Get("power_state").(string)
		LOG.Printf("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)
		}
		js, _ := json.Marshal(servers)
		LOG.Printf("received power state response: %v", string(js))
		ok, id := servers[0].GetStatusID()
		if !ok {
			return fmt.Errorf("Failed extracting power state queue status from: %v", servers[0])
		}
		client.Status.Poll(id, poll)
		status := <-poll
		LOG.Printf("state updated: %v", status)
	}
	d.Partial(false)
	return nil
}
コード例 #3
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)
}