vendor: add github.com/Azure/azure-sdk-for-go and dependencies
This commit is contained in:
+86
@@ -0,0 +1,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 = autorest.NewBearerAuthorizer(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
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"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 main() {
|
||||
resourceGroup := "resourceGroupName"
|
||||
name := "gosdktestname01"
|
||||
|
||||
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 = autorest.NewBearerAuthorizer(spt)
|
||||
|
||||
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("%s is unavailable -- try with another name\n", name)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%s is available\n\n", name)
|
||||
|
||||
cp := storage.AccountCreateParameters{
|
||||
Sku: &storage.Sku{
|
||||
Name: storage.StandardLRS,
|
||||
Tier: storage.Standard},
|
||||
Location: to.StringPtr("westus")}
|
||||
cancel := make(chan struct{})
|
||||
|
||||
_, errchan := ac.Create(resourceGroup, name, cp, cancel)
|
||||
err = <-errchan
|
||||
if err != nil {
|
||||
fmt.Printf("Create '%s' storage account failed: %v\n", name, err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Successfully created '%s' storage account in '%s' resource group\n\n", name, resourceGroup)
|
||||
|
||||
r, err := ac.Delete(resourceGroup, name)
|
||||
if err != nil {
|
||||
fmt.Printf("Delete of '%s' failed with status %s\n...%v\n", name, r.Status, err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Deletion of '%s' storage account in '%s' resource group succeeded -- %s\n", name, resourceGroup, r.Status)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
# Example Accessing the Azure DNS API
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. Create an Azure Resource Group. The code assumes `delete-dns` as the name: `az group create -l westus -n delete-dns`
|
||||
|
||||
2. Create a Service Principal to access the resource group for example with the Azure CLI
|
||||
```
|
||||
az ad sp create-for-rbac --role contributor --scopes /subscriptions/<your subscription id>/resourceGroups/delete-dns
|
||||
|
||||
{
|
||||
"appId": "<appId>",
|
||||
"displayName": "<displayName>",
|
||||
"name": "<name>",
|
||||
"password": "<password>",
|
||||
"tenant": "<tenantId>"
|
||||
}
|
||||
```
|
||||
3. Set the following environment variables from the service principal
|
||||
- AZURE_CLIENT_ID=<appId>
|
||||
- AZURE_CLIENT_SECRET=<password>
|
||||
- AZURE_SUBSCRIPTION_ID=<your subscrption id>
|
||||
- AZURE_TENANT_ID=<tenantId>
|
||||
|
||||
You can query the subscription id for your subscription with: `az account show --query id`
|
||||
|
||||
4. Get the dependencies
|
||||
- go get github.com/Azure/go-autorest/autorest
|
||||
- go get github.com/Azure/go-autorest/autorest/azure
|
||||
- github.com/Azure/azure-sdk-for-go/arm
|
||||
|
||||
## Run the sample
|
||||
|
||||
Execute with `go run create.go`
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/arm/dns"
|
||||
"github.com/Azure/azure-sdk-for-go/arm/examples/helpers"
|
||||
"github.com/Azure/go-autorest/autorest"
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
"github.com/Azure/go-autorest/autorest/to"
|
||||
)
|
||||
|
||||
func main() {
|
||||
resourceGroup := "delete-dns"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
dc := dns.NewZonesClient(c["AZURE_SUBSCRIPTION_ID"])
|
||||
dc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
||||
rc := dns.NewRecordSetsClient(c["AZURE_SUBSCRIPTION_ID"])
|
||||
rc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
||||
|
||||
newZoneName := "xtoph-test.local"
|
||||
|
||||
zoneParam := &dns.Zone{
|
||||
Location: to.StringPtr("global"),
|
||||
ZoneProperties: &dns.ZoneProperties{
|
||||
MaxNumberOfRecordSets: to.Int64Ptr(1),
|
||||
NumberOfRecordSets: to.Int64Ptr(1),
|
||||
// NameServers: [ "my-nameserver.xtoph.com" ]
|
||||
},
|
||||
}
|
||||
|
||||
newZone, err := dc.CreateOrUpdate(resourceGroup, newZoneName, *zoneParam, "", "")
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't create Zone %s Error: %v\n", newZoneName, err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Status: %s\n", newZone.Status)
|
||||
|
||||
fmt.Printf("New Zone Created\n")
|
||||
|
||||
rrparams := &dns.RecordSet{
|
||||
RecordSetProperties: &dns.RecordSetProperties{
|
||||
TTL: to.Int64Ptr(1000),
|
||||
ARecords: &[]dns.ARecord{
|
||||
{Ipv4Address: to.StringPtr("2.2.2.2")},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("Creating A Rec\n")
|
||||
|
||||
aRecName := "newTestARecordFromAPI"
|
||||
_, err = rc.CreateOrUpdate(resourceGroup, newZoneName, aRecName, dns.A, *rrparams, "", "")
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating Cname: %s, %v", newZoneName, err)
|
||||
return
|
||||
}
|
||||
|
||||
cnameparams := &dns.RecordSet{
|
||||
RecordSetProperties: &dns.RecordSetProperties{
|
||||
TTL: to.Int64Ptr(1000),
|
||||
CnameRecord: &dns.CnameRecord{
|
||||
Cname: to.StringPtr("my-new-cname"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("Creating CName\n")
|
||||
cnameRecordName := "newTestCNameRecordFromAPI"
|
||||
_, err = rc.CreateOrUpdate(resourceGroup, newZoneName, cnameRecordName, dns.CNAME, *cnameparams, "", "")
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating Cname: %s, %v", newZoneName, err)
|
||||
return
|
||||
}
|
||||
|
||||
zone, err := dc.Get(resourceGroup, newZoneName)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error getting zone: %s, %v", newZoneName, err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Nameservers for %s \n", *zone.Name)
|
||||
|
||||
for _, ns := range *zone.NameServers {
|
||||
fmt.Printf("%s\n", ns)
|
||||
}
|
||||
|
||||
fmt.Printf("Recordsets for %s \n", *zone.Name)
|
||||
|
||||
var top int32
|
||||
top = 10
|
||||
rrsets, err := rc.ListByDNSZone(resourceGroup, newZoneName, &top)
|
||||
if err != nil {
|
||||
log.Fatalf("Error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, rrset := range *rrsets.Value {
|
||||
fmt.Printf("Recordset: %s Type: %s\n", *rrset.Name, *rrset.Type)
|
||||
|
||||
switch *rrset.Type {
|
||||
case "Microsoft.Network/dnszones/A":
|
||||
printARecords(rrset)
|
||||
case "Microsoft.Network/dnszones/CNAME":
|
||||
printCNames(rrset)
|
||||
case "Microsoft.Network/dnszones/NS":
|
||||
printNS(rrset)
|
||||
case "Microsoft.Network/dnszones/SOA":
|
||||
printSOA(rrset)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("*** Cleaning up *** ")
|
||||
rc.Delete(resourceGroup, newZoneName, cnameRecordName, dns.CNAME, "")
|
||||
rc.Delete(resourceGroup, newZoneName, aRecName, dns.A, "")
|
||||
dc.Delete(resourceGroup, newZoneName, "", nil)
|
||||
fmt.Printf("done\n")
|
||||
|
||||
}
|
||||
|
||||
func printNS(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** NS Record ***\n")
|
||||
if rrset.NsRecords != nil {
|
||||
for _, ns := range *rrset.NsRecords {
|
||||
fmt.Printf("Nameserver: %s\n", *ns.Nsdname)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
}
|
||||
|
||||
func printSOA(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** SOA Record ***\n")
|
||||
|
||||
if rrset.SoaRecord != nil {
|
||||
fmt.Printf("Email: %s\n", *rrset.SoaRecord.Email)
|
||||
fmt.Printf("Host: %s\n", *rrset.SoaRecord.Host)
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func printCNames(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** CNAME Record ***\n")
|
||||
if rrset.CnameRecord != nil {
|
||||
fmt.Printf("Cname %s\n", *rrset.CnameRecord.Cname)
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
|
||||
}
|
||||
func printARecords(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** A Records ***\n")
|
||||
if rrset.ARecords != nil {
|
||||
for _, arec := range *rrset.ARecords {
|
||||
fmt.Printf("record %s\n", *arec.Ipv4Address)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/arm/dns"
|
||||
"github.com/Azure/azure-sdk-for-go/arm/examples/helpers"
|
||||
"github.com/Azure/go-autorest/autorest"
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
"github.com/Azure/go-autorest/autorest/to"
|
||||
)
|
||||
|
||||
func main() {
|
||||
resourceGroup := "delete-dns"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
dc := dns.NewZonesClient(c["AZURE_SUBSCRIPTION_ID"])
|
||||
dc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
||||
rc := dns.NewRecordSetsClient(c["AZURE_SUBSCRIPTION_ID"])
|
||||
rc.Authorizer = autorest.NewBearerAuthorizer(spt)
|
||||
|
||||
newZoneName := "xtoph-test.local"
|
||||
|
||||
zoneParam := &dns.Zone{
|
||||
Location: to.StringPtr("global"),
|
||||
ZoneProperties: &dns.ZoneProperties{
|
||||
MaxNumberOfRecordSets: to.Int64Ptr(1),
|
||||
NumberOfRecordSets: to.Int64Ptr(1),
|
||||
},
|
||||
}
|
||||
|
||||
newZone, err := dc.CreateOrUpdate(resourceGroup, newZoneName, *zoneParam, "", "")
|
||||
if err != nil {
|
||||
log.Fatalf("Couldn't create Zone %s Error: %v\n", newZoneName, err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Status: %s\n", newZone.Status)
|
||||
|
||||
fmt.Printf("New Zone Created\n")
|
||||
|
||||
for i := 0; i < 12; i++ {
|
||||
cnameRecordName := "www1" + strconv.Itoa(i)
|
||||
cnameparams := &dns.RecordSet{
|
||||
Name: &cnameRecordName,
|
||||
ID: &cnameRecordName,
|
||||
RecordSetProperties: &dns.RecordSetProperties{
|
||||
TTL: to.Int64Ptr(1000),
|
||||
CnameRecord: &dns.CnameRecord{
|
||||
Cname: to.StringPtr("alias." + newZoneName),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("Creating CName %q\n", cnameRecordName)
|
||||
_, err = rc.CreateOrUpdate(resourceGroup, newZoneName, cnameRecordName, dns.CNAME, *cnameparams, "", "*")
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating Cname: %s, %v", newZoneName, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
zone, err := dc.Get(resourceGroup, newZoneName)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error getting zone: %s, %v", newZoneName, err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Nameservers for %s \n", *zone.Name)
|
||||
|
||||
for _, ns := range *zone.NameServers {
|
||||
fmt.Printf("%s\n", ns)
|
||||
}
|
||||
|
||||
fmt.Printf("Recordsets for %s \n", *zone.Name)
|
||||
|
||||
var top int32
|
||||
top = 10
|
||||
page := 0
|
||||
rrsets := make([]dns.RecordSet, 0)
|
||||
result, err := rc.ListByDNSZone(resourceGroup, newZoneName, &top)
|
||||
if err != nil {
|
||||
log.Fatalf("Error: %v", err)
|
||||
return
|
||||
}
|
||||
appendListRecordSetsResult(&rrsets, result, &rc, &page)
|
||||
|
||||
printResults(&rrsets)
|
||||
|
||||
fmt.Printf("*** Cleaning up *** ")
|
||||
defer dc.Delete(resourceGroup, newZoneName, "", nil)
|
||||
fmt.Printf("done\n")
|
||||
}
|
||||
|
||||
func printResults(rrsets *[]dns.RecordSet) {
|
||||
for _, rrset := range *rrsets {
|
||||
fmt.Printf("Recordset: %s Type: %s\n", *rrset.Name, *rrset.Type)
|
||||
switch *rrset.Type {
|
||||
case "Microsoft.Network/dnszones/A":
|
||||
printARecords(rrset)
|
||||
case "Microsoft.Network/dnszones/CNAME":
|
||||
printCNames(rrset)
|
||||
case "Microsoft.Network/dnszones/NS":
|
||||
printNS(rrset)
|
||||
case "Microsoft.Network/dnszones/SOA":
|
||||
printSOA(rrset)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("Got %d records\n", len(*rrsets))
|
||||
|
||||
}
|
||||
|
||||
func appendListRecordSetsResult(rrsets *[]dns.RecordSet, result dns.RecordSetListResult, rc *dns.RecordSetsClient, page *int) {
|
||||
for _, rset := range *result.Value {
|
||||
*rrsets = append(*rrsets, rset)
|
||||
}
|
||||
|
||||
if result.NextLink != nil {
|
||||
fmt.Printf("Fetching more from %q\n", *result.NextLink)
|
||||
result, _ := rc.ListByDNSZoneNextResults(result)
|
||||
// TODO Error handling
|
||||
*page++
|
||||
appendListRecordSetsResult(rrsets, result, rc, page)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func printNS(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** NS Record ***\n")
|
||||
if rrset.NsRecords != nil {
|
||||
for _, ns := range *rrset.NsRecords {
|
||||
fmt.Printf("Nameserver: %s\n", *ns.Nsdname)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
}
|
||||
|
||||
func printSOA(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** SOA Record ***\n")
|
||||
|
||||
if rrset.SoaRecord != nil {
|
||||
fmt.Printf("Email: %s\n", *rrset.SoaRecord.Email)
|
||||
fmt.Printf("Host: %s\n", *rrset.SoaRecord.Host)
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func printCNames(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** CNAME Record Name: %q ID: %q ***\n", *rrset.Name, *rrset.ID)
|
||||
if rrset.CnameRecord != nil {
|
||||
fmt.Printf("Cname %s\n", *rrset.CnameRecord.Cname)
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
|
||||
}
|
||||
func printARecords(rrset dns.RecordSet) {
|
||||
fmt.Printf("*** A Record Name: %q ID: %q ***\n", *rrset.Name, *rrset.ID)
|
||||
if rrset.ARecords != nil {
|
||||
for _, arec := range *rrset.ARecords {
|
||||
fmt.Printf("record %s\n", *arec.Ipv4Address)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("*** None ***\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Azure/go-autorest/autorest/adal"
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
)
|
||||
|
||||
const (
|
||||
credentialsPath = "/.azure/credentials.json"
|
||||
)
|
||||
|
||||
// ToJSON returns the passed item as a pretty-printed JSON string. If any JSON error occurs,
|
||||
// it returns the empty string.
|
||||
func ToJSON(v interface{}) (string, error) {
|
||||
j, err := json.MarshalIndent(v, "", " ")
|
||||
return string(j), err
|
||||
}
|
||||
|
||||
// NewServicePrincipalTokenFromCredentials creates a new ServicePrincipalToken using values of the
|
||||
// passed credentials map.
|
||||
func NewServicePrincipalTokenFromCredentials(c map[string]string, scope string) (*adal.ServicePrincipalToken, error) {
|
||||
oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, c["AZURE_TENANT_ID"])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return adal.NewServicePrincipalToken(*oauthConfig, c["AZURE_CLIENT_ID"], c["AZURE_CLIENT_SECRET"], scope)
|
||||
}
|
||||
|
||||
func ensureValueStrings(mapOfInterface map[string]interface{}) map[string]string {
|
||||
mapOfStrings := make(map[string]string)
|
||||
for key, value := range mapOfInterface {
|
||||
mapOfStrings[key] = ensureValueString(value)
|
||||
}
|
||||
return mapOfStrings
|
||||
}
|
||||
|
||||
func ensureValueString(value interface{}) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return v
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user