github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 21 Schema: map[string]*schema.Schema{ 22 "name": { 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 28 "location": { 29 Type: schema.TypeString, 30 Required: true, 31 ForceNew: true, 32 StateFunc: azureRMNormalizeLocation, 33 }, 34 35 "resource_group_name": { 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 41 "sku": { 42 Type: schema.TypeString, 43 Required: true, 44 ForceNew: true, 45 ValidateFunc: validateCdnProfileSku, 46 }, 47 48 "tags": tagsSchema(), 49 }, 50 } 51 } 52 53 func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error { 54 client := meta.(*ArmClient) 55 cdnProfilesClient := client.cdnProfilesClient 56 57 log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.") 58 59 name := d.Get("name").(string) 60 location := d.Get("location").(string) 61 resGroup := d.Get("resource_group_name").(string) 62 sku := d.Get("sku").(string) 63 tags := d.Get("tags").(map[string]interface{}) 64 65 cdnProfile := cdn.ProfileCreateParameters{ 66 Location: &location, 67 Tags: expandTags(tags), 68 Sku: &cdn.Sku{ 69 Name: cdn.SkuName(sku), 70 }, 71 } 72 73 _, err := cdnProfilesClient.Create(name, cdnProfile, resGroup, make(chan struct{})) 74 if err != nil { 75 return err 76 } 77 78 read, err := cdnProfilesClient.Get(name, resGroup) 79 if err != nil { 80 return err 81 } 82 if read.ID == nil { 83 return fmt.Errorf("Cannot read CND 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(name, resGroup) 102 if err != nil { 103 return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err) 104 } 105 if resp.StatusCode == http.StatusNotFound { 106 d.SetId("") 107 return nil 108 } 109 110 if resp.Sku != nil { 111 d.Set("sku", string(resp.Sku.Name)) 112 } 113 114 flattenAndSetTags(d, resp.Tags) 115 116 return nil 117 } 118 119 func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error { 120 cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient 121 122 if !d.HasChange("tags") { 123 return nil 124 } 125 126 name := d.Get("name").(string) 127 resGroup := d.Get("resource_group_name").(string) 128 newTags := d.Get("tags").(map[string]interface{}) 129 130 props := cdn.ProfileUpdateParameters{ 131 Tags: expandTags(newTags), 132 } 133 134 _, err := cdnProfilesClient.Update(name, props, resGroup, make(chan struct{})) 135 if err != nil { 136 return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err) 137 } 138 139 return resourceArmCdnProfileRead(d, meta) 140 } 141 142 func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error { 143 cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient 144 145 id, err := parseAzureResourceID(d.Id()) 146 if err != nil { 147 return err 148 } 149 resGroup := id.ResourceGroup 150 name := id.Path["Profiles"] 151 152 _, err = cdnProfilesClient.DeleteIfExists(name, resGroup, make(chan struct{})) 153 154 return err 155 } 156 157 func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) { 158 value := strings.ToLower(v.(string)) 159 skus := map[string]bool{ 160 "standard_akamai": true, 161 "premium_verizon": true, 162 "standard_verizon": true, 163 } 164 165 if !skus[value] { 166 errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Premium_Verizon, Standard_Verizon or Standard_Akamai")) 167 } 168 return 169 }