forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
86 lines (76 loc) · 2.42 KB
/
check.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/Azure/azure-sdk-for-go/arm/examples/helpers"
"github.com/Azure/azure-sdk-for-go/arm/storage"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
)
func withInspection() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(r *http.Request) (*http.Request, error) {
fmt.Printf("Inspecting Request: %s %s\n", r.Method, r.URL)
return p.Prepare(r)
})
}
}
func byInspecting() autorest.RespondDecorator {
return func(r autorest.Responder) autorest.Responder {
return autorest.ResponderFunc(func(resp *http.Response) error {
fmt.Printf("Inspecting Response: %s for %s %s\n", resp.Status, resp.Request.Method, resp.Request.URL)
return r.Respond(resp)
})
}
}
func main() {
name := "testname01"
c := map[string]string{
"AZURE_CLIENT_ID": os.Getenv("AZURE_CLIENT_ID"),
"AZURE_CLIENT_SECRET": os.Getenv("AZURE_CLIENT_SECRET"),
"AZURE_SUBSCRIPTION_ID": os.Getenv("AZURE_SUBSCRIPTION_ID"),
"AZURE_TENANT_ID": os.Getenv("AZURE_TENANT_ID")}
if err := checkEnvVar(&c); err != nil {
log.Fatalf("Error: %v", err)
return
}
spt, err := helpers.NewServicePrincipalTokenFromCredentials(c, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
log.Fatalf("Error: %v", err)
return
}
ac := storage.NewAccountsClient(c["AZURE_SUBSCRIPTION_ID"])
ac.Authorizer = spt
ac.Sender = autorest.CreateSender(
autorest.WithLogging(log.New(os.Stdout, "sdk-example: ", log.LstdFlags)))
ac.RequestInspector = withInspection()
ac.ResponseInspector = byInspecting()
cna, err := ac.CheckNameAvailability(
storage.AccountCheckNameAvailabilityParameters{
Name: to.StringPtr(name),
Type: to.StringPtr("Microsoft.Storage/storageAccounts")})
if err != nil {
log.Fatalf("Error: %v", err)
return
}
if to.Bool(cna.NameAvailable) {
fmt.Printf("The storage account name '%s' is available\n", name)
} else {
fmt.Printf("The storage account name '%s' is unavailable because %s\n", name, to.String(cna.Message))
}
}
func checkEnvVar(envVars *map[string]string) error {
var missingVars []string
for varName, value := range *envVars {
if value == "" {
missingVars = append(missingVars, varName)
}
}
if len(missingVars) > 0 {
return fmt.Errorf("Missing environment variables %v", missingVars)
}
return nil
}