func (c *Client) CreateSnapshot(id string, remote *kite.Client) error { createSnapshotArgs := &args{ MachineId: id, Provider: c.Provider, } resp, err := remote.Tell("createSnapshot", createSnapshotArgs) if err != nil { return err } var result kloud.ControlResult err = resp.Unmarshal(&result) if err != nil { return err } eArgs := kloud.EventArgs([]kloud.EventArg{ { EventId: createSnapshotArgs.MachineId, Type: "createSnapshot", }, }) return listenEvent(eArgs, machinestate.Running, remote) }
func (c *Client) Stop(id string, remote *kite.Client) error { stopArgs := &args{ MachineId: id, Provider: c.Provider, } resp, err := remote.Tell("stop", stopArgs) if err != nil { return err } var result kloud.ControlResult err = resp.Unmarshal(&result) if err != nil { return err } eArgs := kloud.EventArgs([]kloud.EventArg{ { EventId: stopArgs.MachineId, Type: "stop", }, }) return listenEvent(eArgs, machinestate.Stopped, remote) }
func (c *Client) Build(id string, remote *kite.Client) error { buildArgs := &args{ MachineId: id, Provider: c.Provider, TerraformContext: fmt.Sprintf(` provider "aws" { access_key = "${var.aws_access_key}" secret_key = "${var.aws_secret_key}" region = "%s" } resource "aws_instance" "example" { ami = "ami-d05e75b8" instance_type = "%s" }`, c.region(), c.instanceType()), } resp, err := remote.Tell("build", buildArgs) if err != nil { return err } var result kloud.ControlResult err = resp.Unmarshal(&result) if err != nil { return err } eArgs := kloud.EventArgs([]kloud.EventArg{ { EventId: buildArgs.MachineId, Type: "build", }, }) return listenEvent(eArgs, machinestate.Running, remote) }
func TestTerraformStack(t *testing.T) { t.Parallel() username := "******" groupname := "koding" userData, err := team.CreateUser(username, groupname) if err != nil { t.Fatal(err) } remote := userData.Remote args := &kloud.BootstrapRequest{ Identifiers: userData.Identifiers, GroupName: groupname, } _, err = remote.Tell("bootstrap", args) if err != nil { t.Fatal(err) } defer func() { // now destroy them all args.Destroy = true _, err = remote.Tell("bootstrap", args) if err != nil { t.Error(err) } }() planArgs := &kloud.PlanRequest{ StackTemplateID: userData.StackTemplateId, GroupName: groupname, } resp, err := remote.Tell("plan", planArgs) if err != nil { t.Fatal(err) } var planResult *stackplan.Machines if err := resp.Unmarshal(&planResult); err != nil { t.Fatal(err) } inLabels := func(label string) bool { for _, l := range userData.MachineLabels { if l == label { return true } } return false } for _, machine := range planResult.Machines { if !inLabels(machine.Label) { t.Errorf("plan label: have: %+v got: %s\n", userData.MachineLabels, machine.Label) } if machine.Region != team.region() { t.Errorf("plan region: want: %s got: %s\n", team.region(), machine.Region) } } applyArgs := &kloud.ApplyRequest{ StackID: userData.StackId, GroupName: groupname, } resp, err = remote.Tell("apply", applyArgs) if err != nil { t.Fatal(err) } var result kloud.ControlResult err = resp.Unmarshal(&result) if err != nil { t.Fatal(err) } eArgs := kloud.EventArgs([]kloud.EventArg{ { EventId: userData.StackId, Type: "apply", }, }) if err := listenEvent(eArgs, machinestate.Running, remote); err != nil { t.Error(err) } fmt.Printf("===> STARTED to start/stop the machine with id: %s\n", userData.MachineIds[0].Hex()) if err := team.Stop(userData.MachineIds[0].Hex(), userData.Remote); err != nil { t.Error(err) } if err := team.Start(userData.MachineIds[0].Hex(), userData.Remote); err != nil { t.Error(err) } destroyArgs := &kloud.ApplyRequest{ StackID: userData.StackId, Destroy: true, GroupName: groupname, } resp, err = remote.Tell("apply", destroyArgs) if err != nil { t.Fatal(err) } err = resp.Unmarshal(&result) if err != nil { t.Fatal(err) } eArgs = kloud.EventArgs([]kloud.EventArg{ { EventId: userData.StackId, Type: "apply", }, }) if err := listenEvent(eArgs, machinestate.Terminated, remote); err != nil { t.Error(err) } }