| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "net/http" |
| 8 | "os" |
| 9 | |
| 10 | "github.com/Azure/go-autorest/autorest" |
| 11 | "github.com/hashicorp/go-azure-helpers/authentication" |
| 12 | "github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers" |
| 13 | ) |
| 14 | |
| 15 | func main() { |
| 16 | log.Printf("[DEBUG] Started..") |
| 17 | |
| 18 | // NOTE: fill this in |
| 19 | storageAccountName := "example" |
| 20 | |
| 21 | log.Printf("[DEBUG] Building Client..") |
| 22 | client, err := buildClient() |
| 23 | if err != nil { |
| 24 | panic(fmt.Errorf("Error building client: %s", err)) |
| 25 | } |
| 26 | |
| 27 | ctx := context.TODO() |
| 28 | containerName := "armauth" |
| 29 | input := containers.CreateInput{ |
| 30 | AccessLevel: containers.Private, |
| 31 | MetaData: map[string]string{ |
| 32 | "hello": "world", |
| 33 | }, |
| 34 | } |
| 35 | log.Printf("[DEBUG] Creating Container..") |
| 36 | if _, err := client.ContainersClient.Create(ctx, storageAccountName, containerName, input); err != nil { |
| 37 | panic(fmt.Errorf("Error creating container: %s", err)) |
| 38 | } |
| 39 | |
| 40 | log.Printf("[DEBUG] Retrieving Container..") |
| 41 | container, err := client.ContainersClient.GetProperties(ctx, storageAccountName, containerName) |
| 42 | if err != nil { |
| 43 | panic(fmt.Errorf("Error reading properties for container: %s", err)) |
| 44 | } |
| 45 | |
| 46 | log.Printf("[DEBUG] MetaData: %+v", container.MetaData) |
| 47 | } |
| 48 | |
| 49 | type Client struct { |
| 50 | ContainersClient containers.Client |
| 51 | } |
| 52 | |
| 53 | func buildClient() (*Client, error) { |
| 54 | // we're using github.com/hashicorp/go-azure-helpers since it makes this simpler |
| 55 | // but you can use an Authorizer from github.com/Azure/go-autorest directly too |
| 56 | builder := &authentication.Builder{ |
| 57 | SubscriptionID: os.Getenv("ARM_SUBSCRIPTION_ID"), |
| 58 | ClientID: os.Getenv("ARM_CLIENT_ID"), |
| 59 | ClientSecret: os.Getenv("ARM_CLIENT_SECRET"), |
| 60 | TenantID: os.Getenv("ARM_TENANT_ID"), |
| 61 | Environment: os.Getenv("ARM_ENVIRONMENT"), |
| 62 | |
| 63 | // Feature Toggles |
| 64 | SupportsClientSecretAuth: true, |
| 65 | SupportsAzureCliToken: true, |
| 66 | } |
| 67 | |
| 68 | config, err := builder.Build() |
| 69 | if err != nil { |
| 70 | return nil, fmt.Errorf("Error building AzureRM Client: %s", err) |
| 71 | } |
| 72 | |
| 73 | env, err := authentication.DetermineEnvironment(config.Environment) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | |
| 78 | oauthConfig, err := config.BuildOAuthConfig(env.ActiveDirectoryEndpoint) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | |
| 83 | // OAuthConfigForTenant returns a pointer, which can be nil. |
| 84 | if oauthConfig == nil { |
| 85 | return nil, fmt.Errorf("Unable to configure OAuthConfig for tenant %s", config.TenantID) |
| 86 | } |
| 87 | |
| 88 | // support for HTTP Proxies |
| 89 | sender := autorest.DecorateSender(&http.Client{ |
| 90 | Transport: &http.Transport{ |
| 91 | Proxy: http.ProxyFromEnvironment, |
| 92 | }, |
| 93 | }) |
| 94 | |
| 95 | storageAuth, err := config.GetAuthorizationToken(sender, oauthConfig, "https://storage.azure.com/") |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | |
| 100 | containersClient := containers.New() |
| 101 | containersClient.Client.Authorizer = storageAuth |
| 102 | |
| 103 | result := &Client{ |
| 104 | ContainersClient: containersClient, |
| 105 | } |
| 106 | |
| 107 | return result, nil |
| 108 | } |