Created
July 3, 2017 22:18
-
-
Save echuvyrov/cd8f9c2fcbd90a01e7bace2c523bf915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Testing Web Plan Creation with Go SDK | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"os" | |
"github.com/Azure/azure-sdk-for-go/arm/resources/resources" | |
"github.com/Azure/azure-sdk-for-go/arm/web" | |
"github.com/Azure/go-autorest/autorest" | |
"github.com/Azure/go-autorest/autorest/adal" | |
"github.com/Azure/go-autorest/autorest/azure" | |
) | |
var ( | |
groupName = "testAppPlanGoSDK" | |
location = "westus" | |
groupClient resources.GroupsClient | |
appSvcClient web.AppServicePlansClient | |
) | |
func init() { | |
subscriptionID := getEnvVarOrExit("ARM_SUBSCRIPTION_ID") | |
tenantID := getEnvVarOrExit("ARM_TENANT_ID") | |
clientID := getEnvVarOrExit("ARM_CLIENT_ID") | |
clientSecret := getEnvVarOrExit("ARM_CLIENT_SECRET") | |
oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, tenantID) | |
if err != nil { | |
fmt.Printf("Error getting oAuth %v", err) | |
} | |
spt, err := adal.NewServicePrincipalToken(*oauthConfig, clientID, clientSecret, azure.PublicCloud.ResourceManagerEndpoint) | |
if err != nil { | |
fmt.Printf("Error getting spt %v", err) | |
} | |
auth := autorest.NewBearerAuthorizer(spt) | |
groupClient = resources.NewGroupsClient(subscriptionID) | |
groupClient.Authorizer = auth | |
appSvcClient = web.NewAppServicePlansClient(subscriptionID) | |
appSvcClient.Authorizer = auth | |
appSvcClient.Sender = autorest.CreateSender(withRequestLogging()) | |
} | |
func main() { | |
fmt.Println("Azure Web App Plan Sample") | |
createAppServicePlan("TestAppPlan") | |
} | |
func createAppServicePlan(planName string) { | |
resGroup := "testAppPlanGoSDK" | |
name := "testAppPlan" | |
location := "westus" | |
fmt.Println("\tCreating resource group...") | |
resourceGroupParameters := resources.Group{ | |
Location: &location, | |
} | |
if _, err := groupClient.CreateOrUpdate(groupName, resourceGroupParameters); err != nil { | |
fmt.Printf("Cannot create resource resource group %s, error %v", resGroup, err) | |
return | |
} | |
fmt.Println("Creating an app service plan...") | |
skuname := "S1" | |
tier := "S" | |
size := "S1" | |
sku := web.SkuDescription{ | |
Name: &skuname, | |
Tier: &tier, | |
Size: &size, | |
} | |
properties := web.AppServicePlanProperties{} | |
appServicePlan := web.AppServicePlan{ | |
Location: &location, | |
AppServicePlanProperties: &properties, | |
Sku: &sku, | |
} | |
_, error := appSvcClient.CreateOrUpdate(resGroup, name, appServicePlan, make(chan struct{})) | |
err := <-error | |
if err != nil { | |
fmt.Printf("Cannot create AzureRM App Service Plan %s (resource group %s) ID, %v", name, resGroup, err) | |
return | |
} | |
read, err := appSvcClient.Get(resGroup, name) | |
if err != nil { | |
fmt.Printf("Some error %v", err) | |
} | |
if read.ID == nil { | |
fmt.Printf("Couldn't get plan ID %s", *read.ID) | |
} | |
fmt.Println("Done") | |
} | |
func withRequestLogging() autorest.SendDecorator { | |
return func(s autorest.Sender) autorest.Sender { | |
return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) { | |
// dump request to wire format | |
if dump, err := httputil.DumpRequestOut(r, true); err == nil { | |
log.Printf("[DEBUG] AzureRM Request: \n%s\n", dump) | |
} else { | |
// fallback to basic message | |
log.Printf("[DEBUG] AzureRM Request: %s to %s\n", r.Method, r.URL) | |
} | |
resp, err := s.Do(r) | |
if resp != nil { | |
// dump response to wire format | |
if dump, err := httputil.DumpResponse(resp, true); err == nil { | |
log.Printf("[DEBUG] AzureRM Response for %s: \n%s\n", r.URL, dump) | |
} else { | |
// fallback to basic message | |
log.Printf("[DEBUG] AzureRM Response: %s for %s\n", resp.Status, r.URL) | |
} | |
} else { | |
log.Printf("[DEBUG] Request to %s completed with no response", r.URL) | |
} | |
return resp, err | |
}) | |
} | |
} | |
// getEnvVarOrExit returns the value of specified environment variable or terminates if it's not defined. | |
func getEnvVarOrExit(varName string) string { | |
value := os.Getenv(varName) | |
if value == "" { | |
fmt.Printf("Missing environment variable %s\n", varName) | |
os.Exit(1) | |
} | |
return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment