|
| 1 | +package azurerm |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/Azure/azure-sdk-for-go/arm/cdn" |
| 11 | + "github.com/hashicorp/terraform/helper/resource" |
| 12 | + "github.com/hashicorp/terraform/helper/schema" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceArmCdnProfile() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceArmCdnProfileCreate, |
| 18 | + Read: resourceArmCdnProfileRead, |
| 19 | + Update: resourceArmCdnProfileUpdate, |
| 20 | + Delete: resourceArmCdnProfileDelete, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "name": &schema.Schema{ |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + ForceNew: true, |
| 27 | + }, |
| 28 | + |
| 29 | + "location": &schema.Schema{ |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + StateFunc: azureRMNormalizeLocation, |
| 34 | + }, |
| 35 | + |
| 36 | + "resource_group_name": &schema.Schema{ |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + ForceNew: true, |
| 40 | + }, |
| 41 | + |
| 42 | + "sku": &schema.Schema{ |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + ForceNew: true, |
| 46 | + ValidateFunc: validateCdnProfileSku, |
| 47 | + }, |
| 48 | + |
| 49 | + "tags": tagsSchema(), |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error { |
| 55 | + client := meta.(*ArmClient) |
| 56 | + cdnProfilesClient := client.cdnProfilesClient |
| 57 | + |
| 58 | + log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.") |
| 59 | + |
| 60 | + name := d.Get("name").(string) |
| 61 | + location := d.Get("location").(string) |
| 62 | + resGroup := d.Get("resource_group_name").(string) |
| 63 | + sku := d.Get("sku").(string) |
| 64 | + tags := d.Get("tags").(map[string]interface{}) |
| 65 | + |
| 66 | + properties := cdn.ProfilePropertiesCreateParameters{ |
| 67 | + Sku: &cdn.Sku{ |
| 68 | + Name: cdn.SkuName(sku), |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + cdnProfile := cdn.ProfileCreateParameters{ |
| 73 | + Location: &location, |
| 74 | + Properties: &properties, |
| 75 | + Tags: expandTags(tags), |
| 76 | + } |
| 77 | + |
| 78 | + resp, err := cdnProfilesClient.Create(name, cdnProfile, resGroup) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + d.SetId(*resp.ID) |
| 84 | + |
| 85 | + log.Printf("[DEBUG] Waiting for CDN Profile (%s) to become available", name) |
| 86 | + stateConf := &resource.StateChangeConf{ |
| 87 | + Pending: []string{"Accepted", "Updating", "Creating"}, |
| 88 | + Target: "Succeeded", |
| 89 | + Refresh: cdnProfileStateRefreshFunc(client, resGroup, name), |
| 90 | + Timeout: 10 * time.Minute, |
| 91 | + } |
| 92 | + if _, err := stateConf.WaitForState(); err != nil { |
| 93 | + return fmt.Errorf("Error waiting for CDN Profile (%s) to become available: %s", name, err) |
| 94 | + } |
| 95 | + |
| 96 | + return resourceArmCdnProfileRead(d, meta) |
| 97 | +} |
| 98 | + |
| 99 | +func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error { |
| 100 | + cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient |
| 101 | + |
| 102 | + id, err := parseAzureResourceID(d.Id()) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + resGroup := id.ResourceGroup |
| 107 | + name := id.Path["Profiles"] |
| 108 | + |
| 109 | + resp, err := cdnProfilesClient.Get(name, resGroup) |
| 110 | + if resp.StatusCode == http.StatusNotFound { |
| 111 | + d.SetId("") |
| 112 | + return nil |
| 113 | + } |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err) |
| 116 | + } |
| 117 | + |
| 118 | + if resp.Properties != nil && resp.Properties.Sku != nil { |
| 119 | + d.Set("sku", string(resp.Properties.Sku.Name)) |
| 120 | + } |
| 121 | + |
| 122 | + flattenAndSetTags(d, resp.Tags) |
| 123 | + |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error { |
| 128 | + cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient |
| 129 | + |
| 130 | + if !d.HasChange("tags") { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + name := d.Get("name").(string) |
| 135 | + resGroup := d.Get("resource_group_name").(string) |
| 136 | + newTags := d.Get("tags").(map[string]interface{}) |
| 137 | + |
| 138 | + props := cdn.ProfileUpdateParameters{ |
| 139 | + Tags: expandTags(newTags), |
| 140 | + } |
| 141 | + |
| 142 | + _, err := cdnProfilesClient.Update(name, props, resGroup) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err) |
| 145 | + } |
| 146 | + |
| 147 | + return resourceArmCdnProfileRead(d, meta) |
| 148 | +} |
| 149 | + |
| 150 | +func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error { |
| 151 | + cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient |
| 152 | + |
| 153 | + id, err := parseAzureResourceID(d.Id()) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + resGroup := id.ResourceGroup |
| 158 | + name := id.Path["Profiles"] |
| 159 | + |
| 160 | + _, err = cdnProfilesClient.DeleteIfExists(name, resGroup) |
| 161 | + |
| 162 | + return err |
| 163 | +} |
| 164 | + |
| 165 | +func cdnProfileStateRefreshFunc(client *ArmClient, resourceGroupName string, cdnProfileName string) resource.StateRefreshFunc { |
| 166 | + return func() (interface{}, string, error) { |
| 167 | + res, err := client.cdnProfilesClient.Get(cdnProfileName, resourceGroupName) |
| 168 | + if err != nil { |
| 169 | + return nil, "", fmt.Errorf("Error issuing read request in cdnProfileStateRefreshFunc to Azure ARM for CND Profile '%s' (RG: '%s'): %s", cdnProfileName, resourceGroupName, err) |
| 170 | + } |
| 171 | + return res, string(res.Properties.ProvisioningState), nil |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) { |
| 176 | + value := strings.ToLower(v.(string)) |
| 177 | + skus := map[string]bool{ |
| 178 | + "standard": true, |
| 179 | + "premium": true, |
| 180 | + } |
| 181 | + |
| 182 | + if !skus[value] { |
| 183 | + errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Standard or Premium")) |
| 184 | + } |
| 185 | + return |
| 186 | +} |
0 commit comments