2016-01-19 11:11:37 +01:00
|
|
|
package azurerm
|
|
|
|
|
2016-06-02 02:44:08 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2016-06-30 16:36:08 +02:00
|
|
|
|
2016-06-02 02:44:08 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/cdn"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmCdnProfile() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmCdnProfileCreate,
|
|
|
|
Read: resourceArmCdnProfileRead,
|
|
|
|
Update: resourceArmCdnProfileUpdate,
|
|
|
|
Delete: resourceArmCdnProfileDelete,
|
2016-10-10 14:45:15 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-06-02 02:44:08 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-29 16:54:00 +01:00
|
|
|
"location": locationSchema(),
|
2016-06-02 02:44:08 +02:00
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"sku": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateCdnProfileSku,
|
|
|
|
},
|
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
cdnProfilesClient := client.cdnProfilesClient
|
|
|
|
|
|
|
|
log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
location := d.Get("location").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
sku := d.Get("sku").(string)
|
|
|
|
tags := d.Get("tags").(map[string]interface{})
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
cdnProfile := cdn.Profile{
|
2016-06-30 16:36:08 +02:00
|
|
|
Location: &location,
|
|
|
|
Tags: expandTags(tags),
|
2016-06-02 02:44:08 +02:00
|
|
|
Sku: &cdn.Sku{
|
|
|
|
Name: cdn.SkuName(sku),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
_, err := cdnProfilesClient.Create(resGroup, name, cdnProfile, make(chan struct{}))
|
2016-06-02 02:44:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
read, err := cdnProfilesClient.Get(resGroup, name)
|
2016-06-02 02:44:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read CND Profile %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmCdnProfileRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
2016-10-10 14:45:15 +02:00
|
|
|
name := id.Path["profiles"]
|
2016-06-02 02:44:08 +02:00
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
resp, err := cdnProfilesClient.Get(resGroup, name)
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
if err != nil {
|
2016-09-30 18:57:59 +02:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
|
|
|
|
}
|
2016-06-02 02:44:08 +02:00
|
|
|
|
2016-10-10 14:45:15 +02:00
|
|
|
d.Set("name", name)
|
|
|
|
d.Set("resource_group_name", resGroup)
|
|
|
|
d.Set("location", azureRMNormalizeLocation(*resp.Location))
|
|
|
|
|
2016-06-30 16:36:08 +02:00
|
|
|
if resp.Sku != nil {
|
|
|
|
d.Set("sku", string(resp.Sku.Name))
|
2016-06-02 02:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flattenAndSetTags(d, resp.Tags)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
|
|
|
|
|
|
|
|
if !d.HasChange("tags") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
newTags := d.Get("tags").(map[string]interface{})
|
|
|
|
|
|
|
|
props := cdn.ProfileUpdateParameters{
|
|
|
|
Tags: expandTags(newTags),
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
_, err := cdnProfilesClient.Update(resGroup, name, props, make(chan struct{}))
|
2016-06-02 02:44:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceArmCdnProfileRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
2016-10-10 14:45:15 +02:00
|
|
|
name := id.Path["profiles"]
|
2016-06-02 02:44:08 +02:00
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
_, err = cdnProfilesClient.Delete(resGroup, name, make(chan struct{}))
|
|
|
|
// TODO: check the status code
|
2016-06-02 02:44:08 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := strings.ToLower(v.(string))
|
|
|
|
skus := map[string]bool{
|
2016-06-30 16:36:08 +02:00
|
|
|
"standard_akamai": true,
|
|
|
|
"premium_verizon": true,
|
|
|
|
"standard_verizon": true,
|
2016-06-02 02:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !skus[value] {
|
2016-06-30 16:36:08 +02:00
|
|
|
errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Premium_Verizon, Standard_Verizon or Standard_Akamai"))
|
2016-06-02 02:44:08 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|