-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
containers.go
151 lines (122 loc) · 4.76 KB
/
containers.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
package azure
import (
"context"
"testing"
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2019-05-01/containerregistry"
"github.com/stretchr/testify/require"
)
// ContainerRegistryExists indicates whether the specified container registry exists.
// This function would fail the test if there is an error.
func ContainerRegistryExists(t *testing.T, registryName string, resourceGroupName string, subscriptionID string) bool {
exists, err := ContainerRegistryExistsE(registryName, resourceGroupName, subscriptionID)
require.NoError(t, err)
return exists
}
// ContainerRegistryExistsE indicates whether the specified container registry exists.
func ContainerRegistryExistsE(registryName string, resourceGroupName string, subscriptionID string) (bool, error) {
_, err := GetContainerRegistryE(registryName, resourceGroupName, subscriptionID)
if err != nil {
if ResourceNotFoundErrorExists(err) {
return false, nil
}
return false, err
}
return true, nil
}
// GetContainerRegistry gets the container registry object
// This function would fail the test if there is an error.
func GetContainerRegistry(t *testing.T, registryName string, resGroupName string, subscriptionID string) *containerregistry.Registry {
resource, err := GetContainerRegistryE(registryName, resGroupName, subscriptionID)
require.NoError(t, err)
return resource
}
// GetContainerRegistryE gets the container registry object
func GetContainerRegistryE(registryName string, resGroupName string, subscriptionID string) (*containerregistry.Registry, error) {
rgName, err := getTargetAzureResourceGroupName(resGroupName)
if err != nil {
return nil, err
}
client, err := GetContainerRegistryClientE(subscriptionID)
if err != nil {
return nil, err
}
resource, err := client.Get(context.Background(), rgName, registryName)
if err != nil {
return nil, err
}
return &resource, nil
}
// GetContainerRegistryClientE is a helper function that will setup an Azure Container Registry client on your behalf
func GetContainerRegistryClientE(subscriptionID string) (*containerregistry.RegistriesClient, error) {
// Create an ACR client
registryClient, err := CreateContainerRegistryClientE(subscriptionID)
if err != nil {
return nil, err
}
// Create an authorizer
authorizer, err := NewAuthorizer()
if err != nil {
return nil, err
}
// Attach authorizer to the client
registryClient.Authorizer = *authorizer
return registryClient, nil
}
// ContainerInstanceExists indicates whether the specified container instance exists.
// This function would fail the test if there is an error.
func ContainerInstanceExists(t *testing.T, instanceName string, resourceGroupName string, subscriptionID string) bool {
exists, err := ContainerInstanceExistsE(instanceName, resourceGroupName, subscriptionID)
require.NoError(t, err)
return exists
}
// ContainerInstanceExistsE indicates whether the specified container instance exists.
func ContainerInstanceExistsE(instanceName string, resourceGroupName string, subscriptionID string) (bool, error) {
_, err := GetContainerInstanceE(instanceName, resourceGroupName, subscriptionID)
if err != nil {
if ResourceNotFoundErrorExists(err) {
return false, nil
}
return false, err
}
return true, nil
}
// GetContainerInstance gets the container instance object
// This function would fail the test if there is an error.
func GetContainerInstance(t *testing.T, instanceName string, resGroupName string, subscriptionID string) *containerinstance.ContainerGroup {
instance, err := GetContainerInstanceE(instanceName, resGroupName, subscriptionID)
require.NoError(t, err)
return instance
}
// GetContainerInstanceE gets the container instance object
func GetContainerInstanceE(instanceName string, resGroupName string, subscriptionID string) (*containerinstance.ContainerGroup, error) {
rgName, err := getTargetAzureResourceGroupName(resGroupName)
if err != nil {
return nil, err
}
client, err := GetContainerInstanceClientE(subscriptionID)
if err != nil {
return nil, err
}
instance, err := client.Get(context.Background(), rgName, instanceName)
if err != nil {
return nil, err
}
return &instance, nil
}
// GetContainerInstanceClientE is a helper function that will setup an Azure Container Instance client on your behalf
func GetContainerInstanceClientE(subscriptionID string) (*containerinstance.ContainerGroupsClient, error) {
// Create an ACI client
instanceClient, err := CreateContainerInstanceClientE(subscriptionID)
if err != nil {
return nil, err
}
// Create an authorizer
authorizer, err := NewAuthorizer()
if err != nil {
return nil, err
}
// Attach authorizer to the client
instanceClient.Authorizer = *authorizer
return instanceClient, nil
}