/
utils.go
88 lines (78 loc) · 2.51 KB
/
utils.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package azure
import (
azurecloud "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/juju/errors"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/provider/azure/internal/errorutils"
)
// collectAPIVersions returns a map of the latest API version for each
// possible resource type. This is needed to use the Azure Resource
// Management API, because the API version requested must match the
// type of the resource being manipulated through the API, rather than
// the API version specified statically in the resource client code.
func collectAPIVersions(ctx context.ProviderCallContext, client *armresources.ProvidersClient) (map[string]string, error) {
result := make(map[string]string)
res := client.NewListPager(nil)
for res.More() {
p, err := res.NextPage(ctx)
if err != nil {
return map[string]string{}, errorutils.HandleCredentialError(errors.Trace(err), ctx)
}
providers := p.ProviderListResult
for _, provider := range providers.Value {
for _, resourceType := range provider.ResourceTypes {
key := toValue(provider.Namespace) + "/" + toValue(resourceType.ResourceType)
if len(resourceType.APIVersions) == 0 {
continue
}
// The versions are newest-first.
result[key] = toValue(resourceType.APIVersions[0])
}
}
}
return result, nil
}
func azureCloud(cloudName, apiEndpoint, identityEndpoint string) azurecloud.Configuration {
// Use well known cloud definitions from the SDk if possible.
switch cloudName {
case "azure":
return azurecloud.AzurePublic
case "azure-china":
return azurecloud.AzureChina
case "azure-gov":
return azurecloud.AzureGovernment
}
return azurecloud.Configuration{
ActiveDirectoryAuthorityHost: identityEndpoint,
Services: map[azurecloud.ServiceName]azurecloud.ServiceConfiguration{
azurecloud.ResourceManager: {
Audience: "https://management.core.windows.net/",
Endpoint: apiEndpoint,
},
},
}
}
func toValue[T any](v *T) T {
if v == nil {
return *new(T)
}
return *v
}
func toMap(in map[string]*string) map[string]string {
result := make(map[string]string)
for k, v := range in {
result[k] = toValue(v)
}
return result
}
func toMapPtr(in map[string]string) map[string]*string {
result := make(map[string]*string)
for k, v := range in {
result[k] = to.Ptr(v)
}
return result
}