| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/Azure/go-autorest/autorest" |
| 10 | "github.com/hashicorp/go-azure-helpers/authentication" |
| 11 | "github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers" |
| 12 | ) |
| 13 | |
| 14 | func main() { |
| 15 | log.Printf("[DEBUG] Started..") |
| 16 | |
| 17 | // NOTE: fill this in |
| 18 | storageAccountName := "example" |
| 19 | storageAccountKey := "example" |
| 20 | |
| 21 | log.Printf("[DEBUG] Building Client..") |
| 22 | client, err := buildClient(storageAccountName, storageAccountKey) |
| 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(accountName, accountKey string) (*Client, error) { |
| 54 | env, err := authentication.DetermineEnvironment(os.Getenv("ARM_ENVIRONMENT")) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | storageAuth, err := autorest.NewSharedKeyAuthorizer(accountName, accountKey, autorest.SharedKeyLite) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | |
| 64 | containersClient := containers.NewWithEnvironment(*env) |
| 65 | containersClient.Client.Authorizer = storageAuth |
| 66 | |
| 67 | result := &Client{ |
| 68 | ContainersClient: containersClient, |
| 69 | } |
| 70 | |
| 71 | return result, nil |
| 72 | } |