-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_configuration.go
79 lines (63 loc) · 1.85 KB
/
azure_configuration.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
package main
import (
"github.com/Azure/azure-sdk-for-go/arm/dns"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/azure-sdk-for-go/arm/examples/helpers"
"github.com/Azure/go-autorest/autorest/azure"
"io/ioutil"
"encoding/json"
"errors"
"log"
"fmt"
)
type AzureConfiguration struct {
TenantId string `json:"tenantId,omitempty"`
SubscriptionId string `json:"subscriptionId,omitempty"`
AadClientId string `json:"aadClientId,omitempty"`
AadClientSecret string `json:"aadClientSecret,omitempty"`
DnsZoneResourceGroup string `json:"-"`
}
func (config *AzureConfiguration) ReadFromConfig(path string) (error error) {
log.Println(fmt.Sprintf("Parsing AZURE configuration from %s", path))
jsonData, err := ioutil.ReadFile(path)
if err != nil {
error = err
return
}
err = json.Unmarshal([]byte(jsonData), &config)
if err != nil {
error = err
return
}
if config.TenantId == "" {
error = errors.New("tenantId is empty")
}
if config.SubscriptionId == "" {
error = errors.New("subscriptionId is empty")
}
if config.AadClientId == "" {
error = errors.New("aadClientId is empty")
}
if config.AadClientSecret == "" {
error = errors.New("aadClientSecret is empty")
}
return
}
func (config *AzureConfiguration) GetClient() (rc dns.RecordSetsClient, error error) {
log.Println("Fetching AZURE service principal token")
c := map[string]string{
"AZURE_CLIENT_ID": config.AadClientId,
"AZURE_CLIENT_SECRET": config.AadClientSecret,
"AZURE_SUBSCRIPTION_ID": config.SubscriptionId,
"AZURE_TENANT_ID": config.TenantId,
}
spt, err := helpers.NewServicePrincipalTokenFromCredentials(c, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
error = err
return
}
rc = dns.NewRecordSetsClient(c["AZURE_SUBSCRIPTION_ID"])
rc.Authorizer = autorest.NewBearerAuthorizer(spt)
log.Println(" * successfull")
return
}