-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoci.go
219 lines (209 loc) · 6.68 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"context"
"encoding/base64"
"fmt"
"github.com/oracle/oci-go-sdk/common"
)
import "github.com/oracle/oci-go-sdk/core"
type fdkContext struct {
Provider string `json:"provider"`
Registry string `json:"registry"`
ApiUrl string `json:"api-url"`
CallUrl string `json:"call-url"`
UserID string `json:"oracle.user-id"`
TenantID string `json:"oracle.tenancy-id"`
Fingerprint string `json:"oracle.fingerprint"`
PrivateKey string `json:"oracle.key-file"`
DisableCerts bool `json:"disable-certs"`
CompartmentId string `json:"oracle.compartment-id"`
}
type ociState struct {
VcnID string `json:"vcn_id"`
SubnetID string `json:"subnet_id"`
TestApp string `json:"test_app"`
TestFunc string `json:"test_func"`
}
type Output struct {
Status string `json:"bootstrap_status"`
ociState `json:"oci_state"`
fdkContext `json:"fdk_context"`
}
type RepoRequest struct {
RepoDetails `contributesTo:"body"`
}
type RepoDetails struct {
IsPublic *bool `mandatory:"true" json:"isPublic"`
}
func bootstrap(ctx context.Context, config *Config) (*Output, error) {
decoded, err := base64.StdEncoding.DecodeString(config.PrivateKey)
if err != nil {
return nil, err
}
configProvider := common.NewRawConfigurationProvider(config.TenantID, config.UserID, config.Region, config.Fingerprint, string(decoded), nil)
vcnClient, err := core.NewVirtualNetworkClientWithConfigurationProvider(configProvider)
if err != nil {
return nil, wrap("failed to create vcnclient", err)
}
existingVcns, err := vcnClient.ListVcns(ctx, core.ListVcnsRequest{CompartmentId: common.String(config.CompartmentID)})
if err != nil {
return nil, wrap("failed to list vcns", err)
}
var vcnID string
for _, existingVcn := range existingVcns.Items {
if *existingVcn.DisplayName == config.VCNName {
vcnID = *existingVcn.Id
break
}
}
// VCN not found, create one
if vcnID == "" {
createdVcn, err := vcnClient.CreateVcn(ctx, core.CreateVcnRequest{
CreateVcnDetails: core.CreateVcnDetails{
CompartmentId: common.String(config.CompartmentID),
DisplayName: common.String(config.VCNName),
CidrBlock: common.String("10.0.0.0/16"),
DnsLabel: common.String("testvcn"),
},
})
if err != nil {
return nil, wrap("failed to create vcn", err)
}
vcnID = *createdVcn.Id
}
existingSubnets, err := vcnClient.ListSubnets(ctx, core.ListSubnetsRequest{
CompartmentId: common.String(config.CompartmentID),
VcnId: common.String(vcnID),
})
if err != nil {
return nil, wrap("failed to list subnets", err)
}
var subnetID string
var routeTableID string
for _, existingSubnet := range existingSubnets.Items {
if *existingSubnet.DisplayName == config.SubnetRegional {
subnetID = *existingSubnet.Id
routeTableID = *existingSubnet.RouteTableId
break
}
}
// Subnet does not exist, create one
if subnetID == "" {
createdSubnet, err := vcnClient.CreateSubnet(ctx, core.CreateSubnetRequest{
CreateSubnetDetails: core.CreateSubnetDetails{
DisplayName: common.String(config.SubnetRegional),
CompartmentId: common.String(config.CompartmentID),
VcnId: common.String(vcnID),
CidrBlock: common.String("10.0.1.0/24"),
},
})
if err != nil {
return nil, wrap("failed to create subnet", err)
}
subnetID = *createdSubnet.Id
routeTableID = *createdSubnet.RouteTableId
}
// Create Internet Gateway if it does not exist
existingInternetGateways, err := vcnClient.ListInternetGateways(ctx, core.ListInternetGatewaysRequest{
VcnId: common.String(vcnID),
CompartmentId: common.String(config.CompartmentID),
})
if err != nil {
return nil, wrap("failed to list internet gateways", err)
}
var igID string
for _, existingIG := range existingInternetGateways.Items {
igID = *existingIG.Id
break
}
if igID == "" {
// IG does not exist, create one
createdIG, err := vcnClient.CreateInternetGateway(ctx, core.CreateInternetGatewayRequest{
CreateInternetGatewayDetails: core.CreateInternetGatewayDetails{
CompartmentId: common.String(config.CompartmentID),
DisplayName: common.String("ig"),
VcnId: common.String(vcnID),
IsEnabled: common.Bool(true),
},
})
if err != nil {
return nil, wrap("failed to create internet gateway", err)
}
igID = *createdIG.Id
}
// Add a routing rule to internet gateway, if needed
routeTable, err := vcnClient.GetRouteTable(ctx, core.GetRouteTableRequest{
RtId: common.String(routeTableID),
})
if err != nil {
return nil, wrap("failed to get route table", err)
}
var routingRuleFound bool
for _, rule := range routeTable.RouteRules {
if *rule.NetworkEntityId == igID {
routingRuleFound = true
break
}
}
if !routingRuleFound {
_, err := vcnClient.UpdateRouteTable(ctx, core.UpdateRouteTableRequest{
RtId: common.String(routeTableID),
UpdateRouteTableDetails: core.UpdateRouteTableDetails{
RouteRules: []core.RouteRule{core.RouteRule{
NetworkEntityId: common.String(igID),
Destination: common.String("0.0.0.0/0"),
DestinationType: core.RouteRuleDestinationTypeCidrBlock,
}},
},
})
if err != nil {
return nil, wrap("failed to update route table", err)
}
}
// Try to create repository
baseClient, err := common.NewClientWithConfig(configProvider)
if err != nil {
return nil, err
}
baseClient.Host = "https://iad.ocir.io"
request := common.MakeDefaultHTTPRequest("GET", fmt.Sprintf("/20180419/docker/repos/%s/%s", config.TenantName, config.RepositoryName))
_, err = baseClient.Call(ctx, &request)
if _, ok := err.(common.ServiceError); ok {
request, err := common.MakeDefaultHTTPRequestWithTaggedStruct("POST", fmt.Sprintf("/20180419/docker/repos/%s/%s", config.TenantName, config.RepositoryName), RepoRequest{
RepoDetails: RepoDetails{
IsPublic: common.Bool(true),
},
})
if err != nil {
return nil, wrap("failed to create repo create request", err)
}
_, err = baseClient.Call(ctx, &request)
if err != nil {
return nil, wrap("failed to create repo", err)
}
}
output := &Output{
Status: "succeeded",
ociState: ociState{
SubnetID: subnetID,
VcnID: vcnID,
TestApp: "test-app",
TestFunc: "test-func",
},
fdkContext: fdkContext{
CompartmentId: config.CompartmentID,
//PrivateKey: config.PrivateKey,
Fingerprint: config.Fingerprint,
UserID: config.UserID,
TenantID: config.TenantID,
ApiUrl: fmt.Sprintf("https://functions.%s.oraclecloud.com", config.Region),
DisableCerts: true,
Provider: "oracle",
Registry: fmt.Sprintf("iad.ocir.io/%s/%s", config.TenantName, config.RepositoryName),
},
}
return output, nil
}
func wrap(msg string, err error) error {
return fmt.Errorf("msg: %s, err: %s", msg, err)
}