github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_cdn_profile.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 "strings" 9 10 "github.com/Azure/azure-sdk-for-go/arm/cdn" 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 func resourceArmCdnProfile() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceArmCdnProfileCreate, 17 Read: resourceArmCdnProfileRead, 18 Update: resourceArmCdnProfileUpdate, 19 Delete: resourceArmCdnProfileDelete, 20 Importer: &schema.ResourceImporter{ 21 State: schema.ImportStatePassthrough, 22 }, 23 24 Schema: map[string]*schema.Schema{ 25 "name": { 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 31 "location": locationSchema(), 32 33 "resource_group_name": { 34 Type: schema.TypeString, 35 Required: true, 36 ForceNew: true, 37 }, 38 39 "sku": { 40 Type: schema.TypeString, 41 Required: true, 42 ForceNew: true, 43 ValidateFunc: validateCdnProfileSku, 44 DiffSuppressFunc: ignoreCaseDiffSuppressFunc, 45 }, 46 47 "tags": tagsSchema(), 48 }, 49 } 50 } 51 52 func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error { 53 client := meta.(*ArmClient) 54 cdnProfilesClient := client.cdnProfilesClient 55 56 log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.") 57 58 name := d.Get("name").(string) 59 location := d.Get("location").(string) 60 resGroup := d.Get("resource_group_name").(string) 61 sku := d.Get("sku").(string) 62 tags := d.Get("tags").(map[string]interface{}) 63 64 cdnProfile := cdn.Profile{ 65 Location: &location, 66 Tags: expandTags(tags), 67 Sku: &cdn.Sku{ 68 Name: cdn.SkuName(sku), 69 }, 70 } 71 72 _, error := cdnProfilesClient.Create(resGroup, name, cdnProfile, make(chan struct{})) 73 err := <-error 74 if err != nil { 75 return err 76 } 77 78 read, err := cdnProfilesClient.Get(resGroup, name) 79 if err != nil { 80 return err 81 } 82 if read.ID == nil { 83 return fmt.Errorf("Cannot read CDN Profile %s (resource group %s) ID", name, resGroup) 84 } 85 86 d.SetId(*read.ID) 87 88 return resourceArmCdnProfileRead(d, meta) 89 } 90 91 func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error { 92 cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient 93 94 id, err := parseAzureResourceID(d.Id()) 95 if err != nil { 96 return err 97 } 98 resGroup := id.ResourceGroup 99 name := id.Path["profiles"] 100 101 resp, err := cdnProfilesClient.Get(resGroup, name) 102 if err != nil { 103 if resp.StatusCode == http.StatusNotFound { 104 d.SetId("") 105 return nil 106 } 107 return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err) 108 } 109 110 d.Set("name", name) 111 d.Set("resource_group_name", resGroup) 112 d.Set("location", azureRMNormalizeLocation(*resp.Location)) 113 114 if resp.Sku != nil { 115 d.Set("sku", string(resp.Sku.Name)) 116 } 117 118 flattenAndSetTags(d, resp.Tags) 119 120 return nil 121 } 122 123 func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error { 124 cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient 125 126 if !d.HasChange("tags") { 127 return nil 128 } 129 130 name := d.Get("name").(string) 131 resGroup := d.Get("resource_group_name").(string) 132 newTags := d.Get("tags").(map[string]interface{}) 133 134 props := cdn.ProfileUpdateParameters{ 135 Tags: expandTags(newTags), 136 } 137 138 _, error := cdnProfilesClient.Update(resGroup, name, props, make(chan struct{})) 139 err := <-error 140 if err != nil { 141 return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err) 142 } 143 144 return resourceArmCdnProfileRead(d, meta) 145 } 146 147 func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error { 148 cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient 149 150 id, err := parseAzureResourceID(d.Id()) 151 if err != nil { 152 return err 153 } 154 resGroup := id.ResourceGroup 155 name := id.Path["profiles"] 156 157 _, error := cdnProfilesClient.Delete(resGroup, name, make(chan struct{})) 158 err = <-error 159 // TODO: check the status code 160 161 return err 162 } 163 164 func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) { 165 value := strings.ToLower(v.(string)) 166 skus := map[string]bool{ 167 "standard_akamai": true, 168 "premium_verizon": true, 169 "standard_verizon": true, 170 } 171 172 if !skus[value] { 173 errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Premium_Verizon, Standard_Verizon or Standard_Akamai")) 174 } 175 return 176 }