github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/azure/resource_azure_storage_service.go (about) 1 package azure 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "log" 7 8 "github.com/Azure/azure-sdk-for-go/management" 9 "github.com/Azure/azure-sdk-for-go/management/storageservice" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 // resourceAzureStorageService returns the *schema.Resource associated 14 // to an Azure hosted service. 15 func resourceAzureStorageService() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAzureStorageServiceCreate, 18 Read: resourceAzureStorageServiceRead, 19 Exists: resourceAzureStorageServiceExists, 20 Delete: resourceAzureStorageServiceDelete, 21 22 Schema: map[string]*schema.Schema{ 23 // General attributes: 24 "name": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 // TODO(aznashwan): constrain name in description 29 Description: parameterDescriptions["name"], 30 }, 31 "location": &schema.Schema{ 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 Description: parameterDescriptions["location"], 36 }, 37 "label": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 ForceNew: true, 41 Default: "Made by Terraform.", 42 Description: parameterDescriptions["label"], 43 }, 44 "description": &schema.Schema{ 45 Type: schema.TypeString, 46 Optional: true, 47 ForceNew: true, 48 Description: parameterDescriptions["description"], 49 }, 50 // Functional attributes: 51 "account_type": &schema.Schema{ 52 Type: schema.TypeString, 53 Required: true, 54 ForceNew: true, 55 Description: parameterDescriptions["account_type"], 56 }, 57 "affinity_group": &schema.Schema{ 58 Type: schema.TypeString, 59 Optional: true, 60 ForceNew: true, 61 Description: parameterDescriptions["affinity_group"], 62 }, 63 "properties": &schema.Schema{ 64 Type: schema.TypeMap, 65 Optional: true, 66 ForceNew: true, 67 Elem: schema.TypeString, 68 }, 69 // Computed attributes: 70 "url": &schema.Schema{ 71 Type: schema.TypeString, 72 Computed: true, 73 }, 74 "primary_key": &schema.Schema{ 75 Type: schema.TypeString, 76 Computed: true, 77 }, 78 "secondary_key": &schema.Schema{ 79 Type: schema.TypeString, 80 Computed: true, 81 }, 82 }, 83 } 84 } 85 86 // resourceAzureStorageServiceCreate does all the necessary API calls to 87 // create a new Azure storage service. 88 func resourceAzureStorageServiceCreate(d *schema.ResourceData, meta interface{}) error { 89 azureClient := meta.(*Client) 90 mgmtClient := azureClient.mgmtClient 91 storageServiceClient := storageservice.NewClient(mgmtClient) 92 93 // get all the values: 94 log.Println("[INFO] Creating Azure Storage Service creation parameters.") 95 name := d.Get("name").(string) 96 location := d.Get("location").(string) 97 accountType := storageservice.AccountType(d.Get("account_type").(string)) 98 affinityGroup := d.Get("affinity_group").(string) 99 description := d.Get("description").(string) 100 label := base64.StdEncoding.EncodeToString([]byte(d.Get("label").(string))) 101 var props []storageservice.ExtendedProperty 102 if given := d.Get("properties").(map[string]interface{}); len(given) > 0 { 103 props = []storageservice.ExtendedProperty{} 104 for k, v := range given { 105 props = append(props, storageservice.ExtendedProperty{ 106 Name: k, 107 Value: v.(string), 108 }) 109 } 110 } 111 112 // create parameters and send request: 113 log.Println("[INFO] Sending Storage Service creation request to Azure.") 114 reqID, err := storageServiceClient.CreateStorageService( 115 storageservice.StorageAccountCreateParameters{ 116 ServiceName: name, 117 Location: location, 118 Description: description, 119 Label: label, 120 AffinityGroup: affinityGroup, 121 AccountType: accountType, 122 ExtendedProperties: storageservice.ExtendedPropertyList{ 123 ExtendedProperty: props, 124 }, 125 }) 126 if err != nil { 127 return fmt.Errorf("Failed to create Azure storage service %s: %s", name, err) 128 } 129 err = mgmtClient.WaitForOperation(reqID, nil) 130 if err != nil { 131 return fmt.Errorf("Failed creating storage service %s: %s", name, err) 132 } 133 134 d.SetId(name) 135 return resourceAzureStorageServiceRead(d, meta) 136 } 137 138 // resourceAzureStorageServiceRead does all the necessary API calls to 139 // read the state of the storage service off Azure. 140 func resourceAzureStorageServiceRead(d *schema.ResourceData, meta interface{}) error { 141 azureClient := meta.(*Client) 142 mgmtClient := azureClient.mgmtClient 143 storageServiceClient := storageservice.NewClient(mgmtClient) 144 145 // get our storage service: 146 log.Println("[INFO] Sending query about storage service to Azure.") 147 name := d.Get("name").(string) 148 storsvc, err := storageServiceClient.GetStorageService(name) 149 if err != nil { 150 if !management.IsResourceNotFoundError(err) { 151 return fmt.Errorf("Failed to query about Azure about storage service: %s", err) 152 } else { 153 // it means that the resource has been deleted from Azure 154 // in the meantime and we must remove its associated Resource. 155 d.SetId("") 156 return nil 157 158 } 159 } 160 161 // read values: 162 d.Set("url", storsvc.URL) 163 log.Println("[INFO] Querying keys of Azure storage service.") 164 keys, err := storageServiceClient.GetStorageServiceKeys(name) 165 if err != nil { 166 return fmt.Errorf("Failed querying keys for Azure storage service: %s", err) 167 } 168 d.Set("primary_key", keys.PrimaryKey) 169 d.Set("secondary_key", keys.SecondaryKey) 170 171 return nil 172 } 173 174 // resourceAzureStorageServiceExists does all the necessary API calls to 175 // check if the storage service exists on Azure. 176 func resourceAzureStorageServiceExists(d *schema.ResourceData, meta interface{}) (bool, error) { 177 azureClient, ok := meta.(*Client) 178 if !ok { 179 return false, fmt.Errorf("Failed to convert to *Client, got: %T", meta) 180 } 181 mgmtClient := azureClient.mgmtClient 182 storageServiceClient := storageservice.NewClient(mgmtClient) 183 184 // get our storage service: 185 log.Println("[INFO] Sending query about storage service to Azure.") 186 name := d.Get("name").(string) 187 _, err := storageServiceClient.GetStorageService(name) 188 if err != nil { 189 if !management.IsResourceNotFoundError(err) { 190 return false, fmt.Errorf("Failed to query about Azure about storage service: %s", err) 191 } else { 192 // it means that the resource has been deleted from Azure 193 // in the meantime and we must remove its associated Resource. 194 d.SetId("") 195 return false, nil 196 197 } 198 } 199 200 return true, nil 201 } 202 203 // resourceAzureStorageServiceDelete does all the necessary API calls to 204 // delete the storage service off Azure. 205 func resourceAzureStorageServiceDelete(d *schema.ResourceData, meta interface{}) error { 206 azureClient, ok := meta.(*Client) 207 if !ok { 208 return fmt.Errorf("Failed to convert to *Client, got: %T", meta) 209 } 210 mgmtClient := azureClient.mgmtClient 211 storageClient := storageservice.NewClient(mgmtClient) 212 213 // issue the deletion: 214 name := d.Get("name").(string) 215 log.Println("[INFO] Issuing delete of storage service off Azure.") 216 reqID, err := storageClient.DeleteStorageService(name) 217 if err != nil { 218 return fmt.Errorf("Error whilst issuing deletion of storage service off Azure: %s", err) 219 } 220 err = mgmtClient.WaitForOperation(reqID, nil) 221 if err != nil { 222 return fmt.Errorf("Error whilst deleting storage service off Azure: %s", err) 223 } 224 225 d.SetId("") 226 return nil 227 }