This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathdeployment.go
111 lines (89 loc) · 3.79 KB
/
deployment.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
package aci
import (
"context"
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2015-11-01/subscriptions"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/hashicorp/waypoint-plugin-sdk/component"
)
var _ component.Deployment = (*Deployment)(nil)
var locations []string // accessible locations for the current account
func (d *Deployment) containerInstanceGroupsClient(auth autorest.Authorizer) (*containerinstance.ContainerGroupsClient, error) {
// create a container groups client
containerGroupsClient := containerinstance.NewContainerGroupsClient(d.ContainerGroup.SubscriptionId)
containerGroupsClient.Authorizer = auth
return &containerGroupsClient, nil
}
// init sets up the authorizer and fetches the locations
func (d *Deployment) authenticate(ctx context.Context) (autorest.Authorizer, error) {
// create an authorizer from env vars or Azure Managed Service Identity
//authorizer, err := auth.NewAuthorizerFromCLI()
// first try and create an environment
authorizer, err := auth.NewAuthorizerFromEnvironment()
if err != nil {
return nil, fmt.Errorf("Unable to create subscriptions client: %s", err)
}
// we need to timeout this request as this request never fails when we have
// invalid credentials
timeoutContext, cf := context.WithTimeout(ctx, 15*time.Second)
defer cf()
_, err = d.getLocations(timeoutContext, authorizer)
if err == nil {
return authorizer, nil
}
timeoutContext, cf2 := context.WithTimeout(ctx, 15*time.Second)
defer cf2()
// the environment variable auth has failed fall back to CLI auth
authorizer, err = auth.NewAuthorizerFromCLI()
if err != nil {
return authorizer, err
}
_, err = d.getLocations(timeoutContext, authorizer)
if err == nil {
return authorizer, nil
}
return nil, fmt.Errorf(
"Unable to authenticate with the Azure API, ensure you have your credentials set as environment variables, " +
"or you have logged in using the 'az' command line tool",
)
}
func (d *Deployment) getLocations(ctx context.Context, auth autorest.Authorizer) ([]string, error) {
// create a account client
subscriptionClient := subscriptions.NewClient()
subscriptionClient.Authorizer = auth
llr, err := subscriptionClient.ListLocations(ctx, d.ContainerGroup.SubscriptionId)
if err != nil {
return nil, fmt.Errorf("Unable to list locations for this subscription: %s", err)
}
locs := []string{}
for _, v := range *llr.Value {
locs = append(locs, *v.Name)
}
return locs, nil
}
func (d *Deployment) getContainerGroup(ctx context.Context, auth autorest.Authorizer) (containerinstance.ContainerGroup, error) {
c, err := d.containerInstanceGroupsClient(auth)
if err != nil {
return containerinstance.ContainerGroup{}, fmt.Errorf("Unable to create Container Groups client: %s", err)
}
return c.Get(ctx, d.ContainerGroup.ResourceGroup, d.ContainerGroup.Name)
}
func (d *Deployment) createOrUpdate(ctx context.Context, auth autorest.Authorizer, cg containerinstance.ContainerGroup) (containerinstance.ContainerGroup, error) {
c, err := d.containerInstanceGroupsClient(auth)
if err != nil {
return containerinstance.ContainerGroup{}, fmt.Errorf("Unable to create Container Groups client: %s", err)
}
response, err := c.CreateOrUpdate(ctx, d.ContainerGroup.ResourceGroup, d.ContainerGroup.Name, cg)
if err != nil {
return containerinstance.ContainerGroup{}, fmt.Errorf("Unable to create or update container group: %s", err)
}
// Wait for the container group to be created
err = response.WaitForCompletionRef(ctx, c.Client)
if err != nil {
return containerinstance.ContainerGroup{}, fmt.Errorf("Error waiting for container group creation to complete: %s", err)
}
return response.Result(*c)
}