forked from hashicorp/packer
/
driver_oci.go
117 lines (101 loc) · 3.03 KB
/
driver_oci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package oci
import (
"errors"
"fmt"
client "github.com/hashicorp/packer/builder/oracle/oci/client"
)
// driverOCI implements the Driver interface and communicates with Oracle
// OCI.
type driverOCI struct {
client *client.Client
cfg *Config
}
// NewDriverOCI Creates a new driverOCI with a connected client.
func NewDriverOCI(cfg *Config) (Driver, error) {
client, err := client.NewClient(cfg.AccessCfg)
if err != nil {
return nil, err
}
return &driverOCI{client: client, cfg: cfg}, nil
}
// CreateInstance creates a new compute instance.
func (d *driverOCI) CreateInstance(publicKey string) (string, error) {
params := &client.LaunchInstanceParams{
AvailabilityDomain: d.cfg.AvailabilityDomain,
CompartmentID: d.cfg.CompartmentID,
ImageID: d.cfg.BaseImageID,
Shape: d.cfg.Shape,
SubnetID: d.cfg.SubnetID,
Metadata: map[string]string{
"ssh_authorized_keys": publicKey,
},
}
instance, err := d.client.Compute.Instances.Launch(params)
if err != nil {
return "", err
}
return instance.ID, nil
}
// CreateImage creates a new custom image.
func (d *driverOCI) CreateImage(id string) (client.Image, error) {
params := &client.CreateImageParams{
CompartmentID: d.cfg.CompartmentID,
InstanceID: id,
DisplayName: d.cfg.ImageName,
}
image, err := d.client.Compute.Images.Create(params)
if err != nil {
return client.Image{}, err
}
return image, nil
}
// DeleteImage deletes a custom image.
func (d *driverOCI) DeleteImage(id string) error {
return d.client.Compute.Images.Delete(&client.DeleteImageParams{ID: id})
}
// GetInstanceIP returns the public IP corresponding to the given instance id.
func (d *driverOCI) GetInstanceIP(id string) (string, error) {
// get nvic and cross ref to find pub ip address
vnics, err := d.client.Compute.VNICAttachments.List(
&client.ListVnicAttachmentsParams{
InstanceID: id,
CompartmentID: d.cfg.CompartmentID,
},
)
if err != nil {
return "", err
}
if len(vnics) < 1 {
return "", errors.New("instance has zero VNICs")
}
vnic, err := d.client.Compute.VNICs.Get(&client.GetVNICParams{ID: vnics[0].VNICID})
if err != nil {
return "", fmt.Errorf("Error getting VNIC details: %s", err)
}
return vnic.PublicIP, nil
}
// TerminateInstance terminates a compute instance.
func (d *driverOCI) TerminateInstance(id string) error {
params := &client.TerminateInstanceParams{ID: id}
return d.client.Compute.Instances.Terminate(params)
}
// WaitForImageCreation waits for a provisioning custom image to reach the
// "AVAILABLE" state.
func (d *driverOCI) WaitForImageCreation(id string) error {
return client.NewWaiter().WaitForResourceToReachState(
d.client.Compute.Images,
id,
[]string{"PROVISIONING"},
"AVAILABLE",
)
}
// WaitForInstanceState waits for an instance to reach the a given terminal
// state.
func (d *driverOCI) WaitForInstanceState(id string, waitStates []string, terminalState string) error {
return client.NewWaiter().WaitForResourceToReachState(
d.client.Compute.Instances,
id,
waitStates,
terminalState,
)
}