github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 := azureClient.storageServiceClient 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 storageServiceClient := meta.(*Client).storageServiceClient 142 143 // get our storage service: 144 log.Println("[INFO] Sending query about storage service to Azure.") 145 name := d.Get("name").(string) 146 storsvc, err := storageServiceClient.GetStorageService(name) 147 if err != nil { 148 if !management.IsResourceNotFoundError(err) { 149 return fmt.Errorf("Failed to query about Azure about storage service: %s", err) 150 } else { 151 // it means that the resource has been deleted from Azure 152 // in the meantime and we must remove its associated Resource. 153 d.SetId("") 154 return nil 155 156 } 157 } 158 159 // read values: 160 d.Set("url", storsvc.URL) 161 log.Println("[INFO] Querying keys of Azure storage service.") 162 keys, err := storageServiceClient.GetStorageServiceKeys(name) 163 if err != nil { 164 return fmt.Errorf("Failed querying keys for Azure storage service: %s", err) 165 } 166 d.Set("primary_key", keys.PrimaryKey) 167 d.Set("secondary_key", keys.SecondaryKey) 168 169 return nil 170 } 171 172 // resourceAzureStorageServiceExists does all the necessary API calls to 173 // check if the storage service exists on Azure. 174 func resourceAzureStorageServiceExists(d *schema.ResourceData, meta interface{}) (bool, error) { 175 storageServiceClient := meta.(*Client).storageServiceClient 176 177 // get our storage service: 178 log.Println("[INFO] Sending query about storage service to Azure.") 179 name := d.Get("name").(string) 180 _, err := storageServiceClient.GetStorageService(name) 181 if err != nil { 182 if !management.IsResourceNotFoundError(err) { 183 return false, fmt.Errorf("Failed to query about Azure about storage service: %s", err) 184 } else { 185 // it means that the resource has been deleted from Azure 186 // in the meantime and we must remove its associated Resource. 187 d.SetId("") 188 return false, nil 189 190 } 191 } 192 193 return true, nil 194 } 195 196 // resourceAzureStorageServiceDelete does all the necessary API calls to 197 // delete the storage service off Azure. 198 func resourceAzureStorageServiceDelete(d *schema.ResourceData, meta interface{}) error { 199 azureClient := meta.(*Client) 200 mgmtClient := azureClient.mgmtClient 201 storageServiceClient := azureClient.storageServiceClient 202 203 // issue the deletion: 204 name := d.Get("name").(string) 205 log.Println("[INFO] Issuing delete of storage service off Azure.") 206 reqID, err := storageServiceClient.DeleteStorageService(name) 207 if err != nil { 208 return fmt.Errorf("Error whilst issuing deletion of storage service off Azure: %s", err) 209 } 210 err = mgmtClient.WaitForOperation(reqID, nil) 211 if err != nil { 212 return fmt.Errorf("Error whilst deleting storage service off Azure: %s", err) 213 } 214 215 d.SetId("") 216 return nil 217 }