| 1 |
package azure |
| 2 |
|
| 3 |
// Copyright 2017 Microsoft Corporation |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
"io/ioutil" |
| 21 |
"os" |
| 22 |
"strings" |
| 23 |
) |
| 24 |
|
| 25 |
const ( |
| 26 |
// EnvironmentFilepathName captures the name of the environment variable containing the path to the file |
| 27 |
// to be used while populating the Azure Environment. |
| 28 |
EnvironmentFilepathName = "AZURE_ENVIRONMENT_FILEPATH" |
| 29 |
|
| 30 |
// NotAvailable is used for endpoints and resource IDs that are not available for a given cloud. |
| 31 |
NotAvailable = "N/A" |
| 32 |
) |
| 33 |
|
| 34 |
var environments = map[string]Environment{ |
| 35 |
"AZURECHINACLOUD": ChinaCloud, |
| 36 |
"AZUREGERMANCLOUD": GermanCloud, |
| 37 |
"AZURECLOUD": PublicCloud, |
| 38 |
"AZUREPUBLICCLOUD": PublicCloud, |
| 39 |
"AZUREUSGOVERNMENT": USGovernmentCloud, |
| 40 |
"AZUREUSGOVERNMENTCLOUD": USGovernmentCloud, //TODO: deprecate |
| 41 |
} |
| 42 |
|
| 43 |
// ResourceIdentifier contains a set of Azure resource IDs. |
| 44 |
type ResourceIdentifier struct { |
| 45 |
Graph string `json:"graph"` |
| 46 |
KeyVault string `json:"keyVault"` |
| 47 |
Datalake string `json:"datalake"` |
| 48 |
Batch string `json:"batch"` |
| 49 |
OperationalInsights string `json:"operationalInsights"` |
| 50 |
OSSRDBMS string `json:"ossRDBMS"` |
| 51 |
Storage string `json:"storage"` |
| 52 |
Synapse string `json:"synapse"` |
| 53 |
ServiceBus string `json:"serviceBus"` |
| 54 |
SQLDatabase string `json:"sqlDatabase"` |
| 55 |
CosmosDB string `json:"cosmosDB"` |
| 56 |
ManagedHSM string `json:"managedHSM"` |
| 57 |
MicrosoftGraph string `json:"microsoftGraph"` |
| 58 |
} |
| 59 |
|
| 60 |
// Environment represents a set of endpoints for each of Azure's Clouds. |
| 61 |
type Environment struct { |
| 62 |
Name string `json:"name"` |
| 63 |
ManagementPortalURL string `json:"managementPortalURL"` |
| 64 |
PublishSettingsURL string `json:"publishSettingsURL"` |
| 65 |
ServiceManagementEndpoint string `json:"serviceManagementEndpoint"` |
| 66 |
ResourceManagerEndpoint string `json:"resourceManagerEndpoint"` |
| 67 |
ActiveDirectoryEndpoint string `json:"activeDirectoryEndpoint"` |
| 68 |
GalleryEndpoint string `json:"galleryEndpoint"` |
| 69 |
KeyVaultEndpoint string `json:"keyVaultEndpoint"` |
| 70 |
ManagedHSMEndpoint string `json:"managedHSMEndpoint"` |
| 71 |
GraphEndpoint string `json:"graphEndpoint"` |
| 72 |
ServiceBusEndpoint string `json:"serviceBusEndpoint"` |
| 73 |
BatchManagementEndpoint string `json:"batchManagementEndpoint"` |
| 74 |
MicrosoftGraphEndpoint string `json:"microsoftGraphEndpoint"` |
| 75 |
StorageEndpointSuffix string `json:"storageEndpointSuffix"` |
| 76 |
CosmosDBDNSSuffix string `json:"cosmosDBDNSSuffix"` |
| 77 |
MariaDBDNSSuffix string `json:"mariaDBDNSSuffix"` |
| 78 |
MySQLDatabaseDNSSuffix string `json:"mySqlDatabaseDNSSuffix"` |
| 79 |
PostgresqlDatabaseDNSSuffix string `json:"postgresqlDatabaseDNSSuffix"` |
| 80 |
SQLDatabaseDNSSuffix string `json:"sqlDatabaseDNSSuffix"` |
| 81 |
TrafficManagerDNSSuffix string `json:"trafficManagerDNSSuffix"` |
| 82 |
KeyVaultDNSSuffix string `json:"keyVaultDNSSuffix"` |
| 83 |
ManagedHSMDNSSuffix string `json:"managedHSMDNSSuffix"` |
| 84 |
ServiceBusEndpointSuffix string `json:"serviceBusEndpointSuffix"` |
| 85 |
ServiceManagementVMDNSSuffix string `json:"serviceManagementVMDNSSuffix"` |
| 86 |
ResourceManagerVMDNSSuffix string `json:"resourceManagerVMDNSSuffix"` |
| 87 |
ContainerRegistryDNSSuffix string `json:"containerRegistryDNSSuffix"` |
| 88 |
TokenAudience string `json:"tokenAudience"` |
| 89 |
APIManagementHostNameSuffix string `json:"apiManagementHostNameSuffix"` |
| 90 |
SynapseEndpointSuffix string `json:"synapseEndpointSuffix"` |
| 91 |
DatalakeSuffix string `json:"datalakeSuffix"` |
| 92 |
ResourceIdentifiers ResourceIdentifier `json:"resourceIdentifiers"` |
| 93 |
} |
| 94 |
|
| 95 |
var ( |
| 96 |
// PublicCloud is the default public Azure cloud environment |
| 97 |
PublicCloud = Environment{ |
| 98 |
Name: "AzurePublicCloud", |
| 99 |
ManagementPortalURL: "https://manage.windowsazure.com/", |
| 100 |
PublishSettingsURL: "https://manage.windowsazure.com/publishsettings/index", |
| 101 |
ServiceManagementEndpoint: "https://management.core.windows.net/", |
| 102 |
ResourceManagerEndpoint: "https://management.azure.com/", |
| 103 |
ActiveDirectoryEndpoint: "https://login.microsoftonline.com/", |
| 104 |
GalleryEndpoint: "https://gallery.azure.com/", |
| 105 |
KeyVaultEndpoint: "https://vault.azure.net/", |
| 106 |
ManagedHSMEndpoint: "https://managedhsm.azure.net/", |
| 107 |
GraphEndpoint: "https://graph.windows.net/", |
| 108 |
ServiceBusEndpoint: "https://servicebus.windows.net/", |
| 109 |
BatchManagementEndpoint: "https://batch.core.windows.net/", |
| 110 |
MicrosoftGraphEndpoint: "https://graph.microsoft.com/", |
| 111 |
StorageEndpointSuffix: "core.windows.net", |
| 112 |
CosmosDBDNSSuffix: "documents.azure.com", |
| 113 |
MariaDBDNSSuffix: "mariadb.database.azure.com", |
| 114 |
MySQLDatabaseDNSSuffix: "mysql.database.azure.com", |
| 115 |
PostgresqlDatabaseDNSSuffix: "postgres.database.azure.com", |
| 116 |
SQLDatabaseDNSSuffix: "database.windows.net", |
| 117 |
TrafficManagerDNSSuffix: "trafficmanager.net", |
| 118 |
KeyVaultDNSSuffix: "vault.azure.net", |
| 119 |
ManagedHSMDNSSuffix: "managedhsm.azure.net", |
| 120 |
ServiceBusEndpointSuffix: "servicebus.windows.net", |
| 121 |
ServiceManagementVMDNSSuffix: "cloudapp.net", |
| 122 |
ResourceManagerVMDNSSuffix: "cloudapp.azure.com", |
| 123 |
ContainerRegistryDNSSuffix: "azurecr.io", |
| 124 |
TokenAudience: "https://management.azure.com/", |
| 125 |
APIManagementHostNameSuffix: "azure-api.net", |
| 126 |
SynapseEndpointSuffix: "dev.azuresynapse.net", |
| 127 |
DatalakeSuffix: "azuredatalakestore.net", |
| 128 |
ResourceIdentifiers: ResourceIdentifier{ |
| 129 |
Graph: "https://graph.windows.net/", |
| 130 |
KeyVault: "https://vault.azure.net", |
| 131 |
Datalake: "https://datalake.azure.net/", |
| 132 |
Batch: "https://batch.core.windows.net/", |
| 133 |
OperationalInsights: "https://api.loganalytics.io", |
| 134 |
OSSRDBMS: "https://ossrdbms-aad.database.windows.net", |
| 135 |
Storage: "https://storage.azure.com/", |
| 136 |
Synapse: "https://dev.azuresynapse.net", |
| 137 |
ServiceBus: "https://servicebus.azure.net/", |
| 138 |
SQLDatabase: "https://database.windows.net/", |
| 139 |
CosmosDB: "https://cosmos.azure.com", |
| 140 |
ManagedHSM: "https://managedhsm.azure.net", |
| 141 |
MicrosoftGraph: "https://graph.microsoft.com/", |
| 142 |
}, |
| 143 |
} |
| 144 |
|
| 145 |
// USGovernmentCloud is the cloud environment for the US Government |
| 146 |
USGovernmentCloud = Environment{ |
| 147 |
Name: "AzureUSGovernmentCloud", |
| 148 |
ManagementPortalURL: "https://manage.windowsazure.us/", |
| 149 |
PublishSettingsURL: "https://manage.windowsazure.us/publishsettings/index", |
| 150 |
ServiceManagementEndpoint: "https://management.core.usgovcloudapi.net/", |
| 151 |
ResourceManagerEndpoint: "https://management.usgovcloudapi.net/", |
| 152 |
ActiveDirectoryEndpoint: "https://login.microsoftonline.us/", |
| 153 |
GalleryEndpoint: "https://gallery.usgovcloudapi.net/", |
| 154 |
KeyVaultEndpoint: "https://vault.usgovcloudapi.net/", |
| 155 |
ManagedHSMEndpoint: NotAvailable, |
| 156 |
GraphEndpoint: "https://graph.windows.net/", |
| 157 |
ServiceBusEndpoint: "https://servicebus.usgovcloudapi.net/", |
| 158 |
BatchManagementEndpoint: "https://batch.core.usgovcloudapi.net/", |
| 159 |
MicrosoftGraphEndpoint: "https://graph.microsoft.us/", |
| 160 |
StorageEndpointSuffix: "core.usgovcloudapi.net", |
| 161 |
CosmosDBDNSSuffix: "documents.azure.us", |
| 162 |
MariaDBDNSSuffix: "mariadb.database.usgovcloudapi.net", |
| 163 |
MySQLDatabaseDNSSuffix: "mysql.database.usgovcloudapi.net", |
| 164 |
PostgresqlDatabaseDNSSuffix: "postgres.database.usgovcloudapi.net", |
| 165 |
SQLDatabaseDNSSuffix: "database.usgovcloudapi.net", |
| 166 |
TrafficManagerDNSSuffix: "usgovtrafficmanager.net", |
| 167 |
KeyVaultDNSSuffix: "vault.usgovcloudapi.net", |
| 168 |
ManagedHSMDNSSuffix: NotAvailable, |
| 169 |
ServiceBusEndpointSuffix: "servicebus.usgovcloudapi.net", |
| 170 |
ServiceManagementVMDNSSuffix: "usgovcloudapp.net", |
| 171 |
ResourceManagerVMDNSSuffix: "cloudapp.usgovcloudapi.net", |
| 172 |
ContainerRegistryDNSSuffix: "azurecr.us", |
| 173 |
TokenAudience: "https://management.usgovcloudapi.net/", |
| 174 |
APIManagementHostNameSuffix: "azure-api.us", |
| 175 |
SynapseEndpointSuffix: "dev.azuresynapse.usgovcloudapi.net", |
| 176 |
DatalakeSuffix: NotAvailable, |
| 177 |
ResourceIdentifiers: ResourceIdentifier{ |
| 178 |
Graph: "https://graph.windows.net/", |
| 179 |
KeyVault: "https://vault.usgovcloudapi.net", |
| 180 |
Datalake: NotAvailable, |
| 181 |
Batch: "https://batch.core.usgovcloudapi.net/", |
| 182 |
OperationalInsights: "https://api.loganalytics.us", |
| 183 |
OSSRDBMS: "https://ossrdbms-aad.database.usgovcloudapi.net", |
| 184 |
Storage: "https://storage.azure.com/", |
| 185 |
Synapse: "https://dev.azuresynapse.usgovcloudapi.net", |
| 186 |
ServiceBus: "https://servicebus.azure.net/", |
| 187 |
SQLDatabase: "https://database.usgovcloudapi.net/", |
| 188 |
CosmosDB: "https://cosmos.azure.com", |
| 189 |
ManagedHSM: NotAvailable, |
| 190 |
MicrosoftGraph: "https://graph.microsoft.us/", |
| 191 |
}, |
| 192 |
} |
| 193 |
|
| 194 |
// ChinaCloud is the cloud environment operated in China |
| 195 |
ChinaCloud = Environment{ |
| 196 |
Name: "AzureChinaCloud", |
| 197 |
ManagementPortalURL: "https://manage.chinacloudapi.com/", |
| 198 |
PublishSettingsURL: "https://manage.chinacloudapi.com/publishsettings/index", |
| 199 |
ServiceManagementEndpoint: "https://management.core.chinacloudapi.cn/", |
| 200 |
ResourceManagerEndpoint: "https://management.chinacloudapi.cn/", |
| 201 |
ActiveDirectoryEndpoint: "https://login.chinacloudapi.cn/", |
| 202 |
GalleryEndpoint: "https://gallery.chinacloudapi.cn/", |
| 203 |
KeyVaultEndpoint: "https://vault.azure.cn/", |
| 204 |
ManagedHSMEndpoint: NotAvailable, |
| 205 |
GraphEndpoint: "https://graph.chinacloudapi.cn/", |
| 206 |
ServiceBusEndpoint: "https://servicebus.chinacloudapi.cn/", |
| 207 |
BatchManagementEndpoint: "https://batch.chinacloudapi.cn/", |
| 208 |
MicrosoftGraphEndpoint: "https://microsoftgraph.chinacloudapi.cn/", |
| 209 |
StorageEndpointSuffix: "core.chinacloudapi.cn", |
| 210 |
CosmosDBDNSSuffix: "documents.azure.cn", |
| 211 |
MariaDBDNSSuffix: "mariadb.database.chinacloudapi.cn", |
| 212 |
MySQLDatabaseDNSSuffix: "mysql.database.chinacloudapi.cn", |
| 213 |
PostgresqlDatabaseDNSSuffix: "postgres.database.chinacloudapi.cn", |
| 214 |
SQLDatabaseDNSSuffix: "database.chinacloudapi.cn", |
| 215 |
TrafficManagerDNSSuffix: "trafficmanager.cn", |
| 216 |
KeyVaultDNSSuffix: "vault.azure.cn", |
| 217 |
ManagedHSMDNSSuffix: NotAvailable, |
| 218 |
ServiceBusEndpointSuffix: "servicebus.chinacloudapi.cn", |
| 219 |
ServiceManagementVMDNSSuffix: "chinacloudapp.cn", |
| 220 |
ResourceManagerVMDNSSuffix: "cloudapp.chinacloudapi.cn", |
| 221 |
ContainerRegistryDNSSuffix: "azurecr.cn", |
| 222 |
TokenAudience: "https://management.chinacloudapi.cn/", |
| 223 |
APIManagementHostNameSuffix: "azure-api.cn", |
| 224 |
SynapseEndpointSuffix: "dev.azuresynapse.azure.cn", |
| 225 |
DatalakeSuffix: NotAvailable, |
| 226 |
ResourceIdentifiers: ResourceIdentifier{ |
| 227 |
Graph: "https://graph.chinacloudapi.cn/", |
| 228 |
KeyVault: "https://vault.azure.cn", |
| 229 |
Datalake: NotAvailable, |
| 230 |
Batch: "https://batch.chinacloudapi.cn/", |
| 231 |
OperationalInsights: NotAvailable, |
| 232 |
OSSRDBMS: "https://ossrdbms-aad.database.chinacloudapi.cn", |
| 233 |
Storage: "https://storage.azure.com/", |
| 234 |
Synapse: "https://dev.azuresynapse.net", |
| 235 |
ServiceBus: "https://servicebus.azure.net/", |
| 236 |
SQLDatabase: "https://database.chinacloudapi.cn/", |
| 237 |
CosmosDB: "https://cosmos.azure.com", |
| 238 |
ManagedHSM: NotAvailable, |
| 239 |
MicrosoftGraph: "https://microsoftgraph.chinacloudapi.cn", |
| 240 |
}, |
| 241 |
} |
| 242 |
|
| 243 |
// GermanCloud is the cloud environment operated in Germany |
| 244 |
GermanCloud = Environment{ |
| 245 |
Name: "AzureGermanCloud", |
| 246 |
ManagementPortalURL: "http://portal.microsoftazure.de/", |
| 247 |
PublishSettingsURL: "https://manage.microsoftazure.de/publishsettings/index", |
| 248 |
ServiceManagementEndpoint: "https://management.core.cloudapi.de/", |
| 249 |
ResourceManagerEndpoint: "https://management.microsoftazure.de/", |
| 250 |
ActiveDirectoryEndpoint: "https://login.microsoftonline.de/", |
| 251 |
GalleryEndpoint: "https://gallery.cloudapi.de/", |
| 252 |
KeyVaultEndpoint: "https://vault.microsoftazure.de/", |
| 253 |
ManagedHSMEndpoint: NotAvailable, |
| 254 |
GraphEndpoint: "https://graph.cloudapi.de/", |
| 255 |
ServiceBusEndpoint: "https://servicebus.cloudapi.de/", |
| 256 |
BatchManagementEndpoint: "https://batch.cloudapi.de/", |
| 257 |
MicrosoftGraphEndpoint: NotAvailable, |
| 258 |
StorageEndpointSuffix: "core.cloudapi.de", |
| 259 |
CosmosDBDNSSuffix: "documents.microsoftazure.de", |
| 260 |
MariaDBDNSSuffix: "mariadb.database.cloudapi.de", |
| 261 |
MySQLDatabaseDNSSuffix: "mysql.database.cloudapi.de", |
| 262 |
PostgresqlDatabaseDNSSuffix: "postgres.database.cloudapi.de", |
| 263 |
SQLDatabaseDNSSuffix: "database.cloudapi.de", |
| 264 |
TrafficManagerDNSSuffix: "azuretrafficmanager.de", |
| 265 |
KeyVaultDNSSuffix: "vault.microsoftazure.de", |
| 266 |
ManagedHSMDNSSuffix: NotAvailable, |
| 267 |
ServiceBusEndpointSuffix: "servicebus.cloudapi.de", |
| 268 |
ServiceManagementVMDNSSuffix: "azurecloudapp.de", |
| 269 |
ResourceManagerVMDNSSuffix: "cloudapp.microsoftazure.de", |
| 270 |
ContainerRegistryDNSSuffix: NotAvailable, |
| 271 |
TokenAudience: "https://management.microsoftazure.de/", |
| 272 |
APIManagementHostNameSuffix: NotAvailable, |
| 273 |
SynapseEndpointSuffix: NotAvailable, |
| 274 |
DatalakeSuffix: NotAvailable, |
| 275 |
ResourceIdentifiers: ResourceIdentifier{ |
| 276 |
Graph: "https://graph.cloudapi.de/", |
| 277 |
KeyVault: "https://vault.microsoftazure.de", |
| 278 |
Datalake: NotAvailable, |
| 279 |
Batch: "https://batch.cloudapi.de/", |
| 280 |
OperationalInsights: NotAvailable, |
| 281 |
OSSRDBMS: "https://ossrdbms-aad.database.cloudapi.de", |
| 282 |
Storage: "https://storage.azure.com/", |
| 283 |
Synapse: NotAvailable, |
| 284 |
ServiceBus: "https://servicebus.azure.net/", |
| 285 |
SQLDatabase: "https://database.cloudapi.de/", |
| 286 |
CosmosDB: "https://cosmos.azure.com", |
| 287 |
ManagedHSM: NotAvailable, |
| 288 |
MicrosoftGraph: NotAvailable, |
| 289 |
}, |
| 290 |
} |
| 291 |
) |
| 292 |
|
| 293 |
// EnvironmentFromName returns an Environment based on the common name specified. |
| 294 |
func EnvironmentFromName(name string) (Environment, error) { |
| 295 |
// IMPORTANT |
| 296 |
// As per @radhikagupta5: |
| 297 |
// This is technical debt, fundamentally here because Kubernetes is not currently accepting |
| 298 |
// contributions to the providers. Once that is an option, the provider should be updated to |
| 299 |
// directly call `EnvironmentFromFile`. Until then, we rely on dispatching Azure Stack environment creation |
| 300 |
// from this method based on the name that is provided to us. |
| 301 |
if strings.EqualFold(name, "AZURESTACKCLOUD") { |
| 302 |
return EnvironmentFromFile(os.Getenv(EnvironmentFilepathName)) |
| 303 |
} |
| 304 |
|
| 305 |
name = strings.ToUpper(name) |
| 306 |
env, ok := environments[name] |
| 307 |
if !ok { |
| 308 |
return env, fmt.Errorf("autorest/azure: There is no cloud environment matching the name %q", name) |
| 309 |
} |
| 310 |
|
| 311 |
return env, nil |
| 312 |
} |
| 313 |
|
| 314 |
// EnvironmentFromFile loads an Environment from a configuration file available on disk. |
| 315 |
// This function is particularly useful in the Hybrid Cloud model, where one must define their own |
| 316 |
// endpoints. |
| 317 |
func EnvironmentFromFile(location string) (unmarshaled Environment, err error) { |
| 318 |
fileContents, err := ioutil.ReadFile(location) |
| 319 |
if err != nil { |
| 320 |
return |
| 321 |
} |
| 322 |
|
| 323 |
err = json.Unmarshal(fileContents, &unmarshaled) |
| 324 |
|
| 325 |
return |
| 326 |
} |
| 327 |
|
| 328 |
// SetEnvironment updates the environment map with the specified values. |
| 329 |
func SetEnvironment(name string, env Environment) { |
| 330 |
environments[strings.ToUpper(name)] = env |
| 331 |
} |