diff --git a/builder/oracle/classic/step_create_IP_reservation.go b/builder/oracle/classic/step_create_IP_reservation.go index 415b3ddac..797063b1c 100644 --- a/builder/oracle/classic/step_create_IP_reservation.go +++ b/builder/oracle/classic/step_create_IP_reservation.go @@ -29,6 +29,7 @@ func (s *stepCreateIPReservation) Run(state multistep.StateBag) multistep.StepAc log.Printf("Error creating IP Reservation: %s", err) return multistep.ActionHalt } + state.Put("instance_ip", ipRes.IP) log.Printf("debug: ipRes is %#v", ipRes) return multistep.ActionContinue } diff --git a/builder/oracle/classic/step_create_instance.go b/builder/oracle/classic/step_create_instance.go index e461dd5ab..f94d6d0b7 100644 --- a/builder/oracle/classic/step_create_instance.go +++ b/builder/oracle/classic/step_create_instance.go @@ -1,65 +1,37 @@ package classic import ( - "bytes" "fmt" - "text/template" + "github.com/hashicorp/go-oracle-terraform/compute" "github.com/hashicorp/packer/packer" "github.com/mitchellh/multistep" ) -type instanceOptions struct { - Username string - IdentityDomain string - SshKey string - Shape string - ImageList string - InstanceIP string -} - -var instanceTemplate = template.Must(template.New("instanceRequestBody").Parse(` -{ - "instances": [{ - "shape": "{{.Shape}}", - "sshkeys": ["/Compute-{{.IdentityDomain}}/{{Username}}/{{.SshKey}}"], - "name": "Compute-{{.IdentityDomain}}/{{Username}}/packer-instance", - "label": "packer-instance", - "imagelist": "/Compute-{{.IdentityDomain}}/{{Username}}/{{.ImageList}}", - "networking": { - "eth0": { - "nat": "ipreservation:/Compute-{{.IdentityDomain}}/{{Username}}/{{.InstanceIP}}" - } - } - }] -} -`)) - type stepCreateInstance struct{} -func (s *stepCreateIPReservation) Run(state multistep.StateBag) multistep.StepAction { - ui := state.Get("ui").(packer.Ui) - config := state.Get("config").(Config) - const endpoint_path = "/launchplan/" // POST - +func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction { ui.Say("Creating Instance...") - // generate launch plan definition for this instance - var buffer bytes.Buffer - err = instanceTemplate.Execute(&buffer, instanceOptions{ - Username: config.Username, - IdentityDomain: config.IdentityDomain, - SshKey: config.SshKey, - Shape: config.Shape, - ImageList: config.ImageList, - }) - if err != nil { - fmt.Printf("Error creating launch plan definition: %s", err) - return "", err + // get variables from state + ui := state.Get("ui").(packer.Ui) + config := state.Get("config").(Config) + client := state.Get("client").(*compute.ComputeClient) + sshPublicKey := state.Get("publicKey").(string) + + // get instances client + instanceClient := client.Instances() + + // Instances Input + input := &compute.CreateInstanceInput{ + Name: config.ImageName, + Shape: config.Shape, + ImageList: config.ImageList, + SSHKeys: []string{}, + Attributes: map[string]interface{}{}, } - // for API docs see - // https://docs.oracle.com/en/cloud/iaas/compute-iaas-cloud/stcsa/op-launchplan--post.html - instanceID, err := client.CreateInstance(publicKey) + + instanceInfo, err := instanceClient.CreateInstance(input) if err != nil { err = fmt.Errorf("Problem creating instance: %s", err) ui.Error(err.Error()) @@ -67,7 +39,7 @@ func (s *stepCreateIPReservation) Run(state multistep.StateBag) multistep.StepAc return multistep.ActionHalt } - state.Put("instance_id", instanceID) + state.Put("instance_id", instanceInfo.ID) ui.Say(fmt.Sprintf("Created instance (%s).", instanceID)) } diff --git a/builder/oracle/classic/step_snapshot.go b/builder/oracle/classic/step_snapshot.go new file mode 100644 index 000000000..582966a83 --- /dev/null +++ b/builder/oracle/classic/step_snapshot.go @@ -0,0 +1,39 @@ +package classic + +import ( + "fmt" + + "github.com/hashicorp/go-oracle-terraform/compute" + "github.com/hashicorp/packer/packer" + "github.com/mitchellh/multistep" +) + +type stepSnapshot struct{} + +func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction { + // get variables from state + ui := state.Get("ui").(packer.Ui) + ui.Say("Creating Snapshot...") + config := state.Get("config").(Config) + client := state.Get("client").(*compute.ComputeClient) + instanceID := state.Get("instance_id").(string) + + // get instances client + snapshotClient := client.SnapshotsClient() + + // Instances Input + createSnapshotInput := &CreateSnapshotInput{ + Instance: instanceID, + MachineImage: config.ImageName, + } + + snap, err := snapshotClient.CreateSnapshot(input) + if err != nil { + err = fmt.Errorf("Problem creating snapshot: %s", err) + ui.Error(err.Error()) + state.Put("error", err) + return multistep.ActionHalt + } + + ui.Say(fmt.Sprintf("Created snapshot (%s).", snap.Name)) +}