github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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": {
    32  				Type:      schema.TypeString,
    33  				Required:  true,
    34  				ForceNew:  true,
    35  				StateFunc: azureRMNormalizeLocation,
    36  			},
    37  
    38  			"resource_group_name": {
    39  				Type:     schema.TypeString,
    40  				Required: true,
    41  				ForceNew: true,
    42  			},
    43  
    44  			"sku": {
    45  				Type:         schema.TypeString,
    46  				Required:     true,
    47  				ForceNew:     true,
    48  				ValidateFunc: validateCdnProfileSku,
    49  			},
    50  
    51  			"tags": tagsSchema(),
    52  		},
    53  	}
    54  }
    55  
    56  func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error {
    57  	client := meta.(*ArmClient)
    58  	cdnProfilesClient := client.cdnProfilesClient
    59  
    60  	log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")
    61  
    62  	name := d.Get("name").(string)
    63  	location := d.Get("location").(string)
    64  	resGroup := d.Get("resource_group_name").(string)
    65  	sku := d.Get("sku").(string)
    66  	tags := d.Get("tags").(map[string]interface{})
    67  
    68  	cdnProfile := cdn.ProfileCreateParameters{
    69  		Location: &location,
    70  		Tags:     expandTags(tags),
    71  		Sku: &cdn.Sku{
    72  			Name: cdn.SkuName(sku),
    73  		},
    74  	}
    75  
    76  	_, err := cdnProfilesClient.Create(name, cdnProfile, resGroup, make(chan struct{}))
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	read, err := cdnProfilesClient.Get(name, resGroup)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	if read.ID == nil {
    86  		return fmt.Errorf("Cannot read CND Profile %s (resource group %s) ID", name, resGroup)
    87  	}
    88  
    89  	d.SetId(*read.ID)
    90  
    91  	return resourceArmCdnProfileRead(d, meta)
    92  }
    93  
    94  func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
    95  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
    96  
    97  	id, err := parseAzureResourceID(d.Id())
    98  	if err != nil {
    99  		return err
   100  	}
   101  	resGroup := id.ResourceGroup
   102  	name := id.Path["profiles"]
   103  
   104  	resp, err := cdnProfilesClient.Get(name, resGroup)
   105  	if err != nil {
   106  		if resp.StatusCode == http.StatusNotFound {
   107  			d.SetId("")
   108  			return nil
   109  		}
   110  		return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
   111  	}
   112  
   113  	d.Set("name", name)
   114  	d.Set("resource_group_name", resGroup)
   115  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   116  
   117  	if resp.Sku != nil {
   118  		d.Set("sku", string(resp.Sku.Name))
   119  	}
   120  
   121  	flattenAndSetTags(d, resp.Tags)
   122  
   123  	return nil
   124  }
   125  
   126  func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error {
   127  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
   128  
   129  	if !d.HasChange("tags") {
   130  		return nil
   131  	}
   132  
   133  	name := d.Get("name").(string)
   134  	resGroup := d.Get("resource_group_name").(string)
   135  	newTags := d.Get("tags").(map[string]interface{})
   136  
   137  	props := cdn.ProfileUpdateParameters{
   138  		Tags: expandTags(newTags),
   139  	}
   140  
   141  	_, err := cdnProfilesClient.Update(name, props, resGroup, make(chan struct{}))
   142  	if err != nil {
   143  		return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err)
   144  	}
   145  
   146  	return resourceArmCdnProfileRead(d, meta)
   147  }
   148  
   149  func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error {
   150  	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient
   151  
   152  	id, err := parseAzureResourceID(d.Id())
   153  	if err != nil {
   154  		return err
   155  	}
   156  	resGroup := id.ResourceGroup
   157  	name := id.Path["profiles"]
   158  
   159  	_, err = cdnProfilesClient.DeleteIfExists(name, resGroup, make(chan struct{}))
   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  }