...
1 package azure
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "net/http"
8 "strings"
9
10 "github.com/Azure/go-autorest/autorest"
11 )
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 type audience []string
28
29 type authentication struct {
30 LoginEndpoint string `json:"loginEndpoint"`
31 Audiences audience `json:"audiences"`
32 }
33
34 type environmentMetadataInfo struct {
35 GalleryEndpoint string `json:"galleryEndpoint"`
36 GraphEndpoint string `json:"graphEndpoint"`
37 PortalEndpoint string `json:"portalEndpoint"`
38 Authentication authentication `json:"authentication"`
39 }
40
41
42 type EnvironmentProperty string
43
44 const (
45
46 EnvironmentName EnvironmentProperty = "name"
47
48 EnvironmentManagementPortalURL EnvironmentProperty = "managementPortalURL"
49
50 EnvironmentPublishSettingsURL EnvironmentProperty = "publishSettingsURL"
51
52 EnvironmentServiceManagementEndpoint EnvironmentProperty = "serviceManagementEndpoint"
53
54 EnvironmentResourceManagerEndpoint EnvironmentProperty = "resourceManagerEndpoint"
55
56 EnvironmentActiveDirectoryEndpoint EnvironmentProperty = "activeDirectoryEndpoint"
57
58 EnvironmentGalleryEndpoint EnvironmentProperty = "galleryEndpoint"
59
60 EnvironmentKeyVaultEndpoint EnvironmentProperty = "keyVaultEndpoint"
61
62 EnvironmentGraphEndpoint EnvironmentProperty = "graphEndpoint"
63
64 EnvironmentServiceBusEndpoint EnvironmentProperty = "serviceBusEndpoint"
65
66 EnvironmentBatchManagementEndpoint EnvironmentProperty = "batchManagementEndpoint"
67
68 EnvironmentStorageEndpointSuffix EnvironmentProperty = "storageEndpointSuffix"
69
70 EnvironmentSQLDatabaseDNSSuffix EnvironmentProperty = "sqlDatabaseDNSSuffix"
71
72 EnvironmentTrafficManagerDNSSuffix EnvironmentProperty = "trafficManagerDNSSuffix"
73
74 EnvironmentKeyVaultDNSSuffix EnvironmentProperty = "keyVaultDNSSuffix"
75
76 EnvironmentServiceBusEndpointSuffix EnvironmentProperty = "serviceBusEndpointSuffix"
77
78 EnvironmentServiceManagementVMDNSSuffix EnvironmentProperty = "serviceManagementVMDNSSuffix"
79
80 EnvironmentResourceManagerVMDNSSuffix EnvironmentProperty = "resourceManagerVMDNSSuffix"
81
82 EnvironmentContainerRegistryDNSSuffix EnvironmentProperty = "containerRegistryDNSSuffix"
83
84 EnvironmentTokenAudience EnvironmentProperty = "tokenAudience"
85 )
86
87
88 type OverrideProperty struct {
89 Key EnvironmentProperty
90 Value string
91 }
92
93
94
95
96 func EnvironmentFromURL(resourceManagerEndpoint string, properties ...OverrideProperty) (environment Environment, err error) {
97 var metadataEnvProperties environmentMetadataInfo
98
99 if resourceManagerEndpoint == "" {
100 return environment, fmt.Errorf("Metadata resource manager endpoint is empty")
101 }
102
103 if metadataEnvProperties, err = retrieveMetadataEnvironment(resourceManagerEndpoint); err != nil {
104 return environment, err
105 }
106
107
108 overrideProperties(&environment, properties)
109
110 if environment.Name == "" {
111 environment.Name = "HybridEnvironment"
112 }
113 stampDNSSuffix := environment.StorageEndpointSuffix
114 if stampDNSSuffix == "" {
115 stampDNSSuffix = strings.TrimSuffix(strings.TrimPrefix(strings.Replace(resourceManagerEndpoint, strings.Split(resourceManagerEndpoint, ".")[0], "", 1), "."), "/")
116 environment.StorageEndpointSuffix = stampDNSSuffix
117 }
118 if environment.KeyVaultDNSSuffix == "" {
119 environment.KeyVaultDNSSuffix = fmt.Sprintf("%s.%s", "vault", stampDNSSuffix)
120 }
121 if environment.KeyVaultEndpoint == "" {
122 environment.KeyVaultEndpoint = fmt.Sprintf("%s%s", "https://", environment.KeyVaultDNSSuffix)
123 }
124 if environment.TokenAudience == "" {
125 environment.TokenAudience = metadataEnvProperties.Authentication.Audiences[0]
126 }
127 if environment.ActiveDirectoryEndpoint == "" {
128 environment.ActiveDirectoryEndpoint = metadataEnvProperties.Authentication.LoginEndpoint
129 }
130 if environment.ResourceManagerEndpoint == "" {
131 environment.ResourceManagerEndpoint = resourceManagerEndpoint
132 }
133 if environment.GalleryEndpoint == "" {
134 environment.GalleryEndpoint = metadataEnvProperties.GalleryEndpoint
135 }
136 if environment.GraphEndpoint == "" {
137 environment.GraphEndpoint = metadataEnvProperties.GraphEndpoint
138 }
139
140 return environment, nil
141 }
142
143 func overrideProperties(environment *Environment, properties []OverrideProperty) {
144 for _, property := range properties {
145 switch property.Key {
146 case EnvironmentName:
147 {
148 environment.Name = property.Value
149 }
150 case EnvironmentManagementPortalURL:
151 {
152 environment.ManagementPortalURL = property.Value
153 }
154 case EnvironmentPublishSettingsURL:
155 {
156 environment.PublishSettingsURL = property.Value
157 }
158 case EnvironmentServiceManagementEndpoint:
159 {
160 environment.ServiceManagementEndpoint = property.Value
161 }
162 case EnvironmentResourceManagerEndpoint:
163 {
164 environment.ResourceManagerEndpoint = property.Value
165 }
166 case EnvironmentActiveDirectoryEndpoint:
167 {
168 environment.ActiveDirectoryEndpoint = property.Value
169 }
170 case EnvironmentGalleryEndpoint:
171 {
172 environment.GalleryEndpoint = property.Value
173 }
174 case EnvironmentKeyVaultEndpoint:
175 {
176 environment.KeyVaultEndpoint = property.Value
177 }
178 case EnvironmentGraphEndpoint:
179 {
180 environment.GraphEndpoint = property.Value
181 }
182 case EnvironmentServiceBusEndpoint:
183 {
184 environment.ServiceBusEndpoint = property.Value
185 }
186 case EnvironmentBatchManagementEndpoint:
187 {
188 environment.BatchManagementEndpoint = property.Value
189 }
190 case EnvironmentStorageEndpointSuffix:
191 {
192 environment.StorageEndpointSuffix = property.Value
193 }
194 case EnvironmentSQLDatabaseDNSSuffix:
195 {
196 environment.SQLDatabaseDNSSuffix = property.Value
197 }
198 case EnvironmentTrafficManagerDNSSuffix:
199 {
200 environment.TrafficManagerDNSSuffix = property.Value
201 }
202 case EnvironmentKeyVaultDNSSuffix:
203 {
204 environment.KeyVaultDNSSuffix = property.Value
205 }
206 case EnvironmentServiceBusEndpointSuffix:
207 {
208 environment.ServiceBusEndpointSuffix = property.Value
209 }
210 case EnvironmentServiceManagementVMDNSSuffix:
211 {
212 environment.ServiceManagementVMDNSSuffix = property.Value
213 }
214 case EnvironmentResourceManagerVMDNSSuffix:
215 {
216 environment.ResourceManagerVMDNSSuffix = property.Value
217 }
218 case EnvironmentContainerRegistryDNSSuffix:
219 {
220 environment.ContainerRegistryDNSSuffix = property.Value
221 }
222 case EnvironmentTokenAudience:
223 {
224 environment.TokenAudience = property.Value
225 }
226 }
227 }
228 }
229
230 func retrieveMetadataEnvironment(endpoint string) (environment environmentMetadataInfo, err error) {
231 client := autorest.NewClientWithUserAgent("")
232 managementEndpoint := fmt.Sprintf("%s%s", strings.TrimSuffix(endpoint, "/"), "/metadata/endpoints?api-version=1.0")
233 req, _ := http.NewRequest("GET", managementEndpoint, nil)
234 response, err := client.Do(req)
235 if err != nil {
236 return environment, err
237 }
238 defer response.Body.Close()
239 jsonResponse, err := ioutil.ReadAll(response.Body)
240 if err != nil {
241 return environment, err
242 }
243 err = json.Unmarshal(jsonResponse, &environment)
244 return environment, err
245 }
246
View as plain text