github.com/kwoods/terraform@v0.6.11-0.20160809170336-13497db7138e/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceArmTrafficManagerEndpoint() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmTrafficManagerEndpointCreate,
    15  		Read:   resourceArmTrafficManagerEndpointRead,
    16  		Update: resourceArmTrafficManagerEndpointCreate,
    17  		Delete: resourceArmTrafficManagerEndpointDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"type": {
    30  				Type:         schema.TypeString,
    31  				Required:     true,
    32  				ForceNew:     true,
    33  				ValidateFunc: validateAzureRMTrafficManagerEndpointType,
    34  			},
    35  
    36  			"profile_name": {
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  
    42  			"target": {
    43  				Type:     schema.TypeString,
    44  				Optional: true,
    45  				// when targeting an Azure resource the FQDN of that resource will be set as the target
    46  				Computed: true,
    47  			},
    48  
    49  			"target_resource_id": {
    50  				Type:     schema.TypeString,
    51  				Optional: true,
    52  			},
    53  
    54  			"endpoint_status": {
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  				Computed: true,
    58  			},
    59  
    60  			"weight": {
    61  				Type:         schema.TypeInt,
    62  				Optional:     true,
    63  				Computed:     true,
    64  				ValidateFunc: validateAzureRMTrafficManagerEndpointWeight,
    65  			},
    66  
    67  			"priority": {
    68  				Type:         schema.TypeInt,
    69  				Optional:     true,
    70  				Computed:     true,
    71  				ValidateFunc: validateAzureRMTrafficManagerEndpointPriority,
    72  			},
    73  
    74  			"endpoint_location": {
    75  				Type:     schema.TypeString,
    76  				Optional: true,
    77  				// when targeting an Azure resource the location of that resource will be set on the endpoint
    78  				Computed:  true,
    79  				StateFunc: azureRMNormalizeLocation,
    80  			},
    81  
    82  			"min_child_endpoints": {
    83  				Type:     schema.TypeInt,
    84  				Optional: true,
    85  			},
    86  
    87  			"resource_group_name": {
    88  				Type:     schema.TypeString,
    89  				Required: true,
    90  				ForceNew: true,
    91  			},
    92  		},
    93  	}
    94  }
    95  
    96  func resourceArmTrafficManagerEndpointCreate(d *schema.ResourceData, meta interface{}) error {
    97  	client := meta.(*ArmClient).trafficManagerEndpointsClient
    98  
    99  	log.Printf("[INFO] preparing arguments for ARM TrafficManager Endpoint creation.")
   100  
   101  	name := d.Get("name").(string)
   102  	endpointType := d.Get("type").(string)
   103  	fullEndpointType := fmt.Sprintf("Microsoft.Network/TrafficManagerProfiles/%s", endpointType)
   104  	profileName := d.Get("profile_name").(string)
   105  	resGroup := d.Get("resource_group_name").(string)
   106  
   107  	params := trafficmanager.Endpoint{
   108  		Name:       &name,
   109  		Type:       &fullEndpointType,
   110  		Properties: getArmTrafficManagerEndpointProperties(d),
   111  	}
   112  
   113  	_, err := client.CreateOrUpdate(resGroup, profileName, endpointType, name, params)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	read, err := client.Get(resGroup, profileName, endpointType, name)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	if read.ID == nil {
   123  		return fmt.Errorf("Cannot read TrafficManager endpoint %s (resource group %s) ID", name, resGroup)
   124  	}
   125  
   126  	d.SetId(*read.ID)
   127  
   128  	return resourceArmTrafficManagerEndpointRead(d, meta)
   129  }
   130  
   131  func resourceArmTrafficManagerEndpointRead(d *schema.ResourceData, meta interface{}) error {
   132  	client := meta.(*ArmClient).trafficManagerEndpointsClient
   133  
   134  	id, err := parseAzureResourceID(d.Id())
   135  	if err != nil {
   136  		return err
   137  	}
   138  	resGroup := id.ResourceGroup
   139  	endpointType := d.Get("type").(string)
   140  	profileName := id.Path["trafficManagerProfiles"]
   141  
   142  	// endpoint name is keyed by endpoint type in ARM ID
   143  	name := id.Path[endpointType]
   144  
   145  	resp, err := client.Get(resGroup, profileName, endpointType, name)
   146  	if resp.StatusCode == http.StatusNotFound {
   147  		d.SetId("")
   148  		return nil
   149  	}
   150  	if err != nil {
   151  		return fmt.Errorf("Error making Read request on TrafficManager Endpoint %s: %s", name, err)
   152  	}
   153  	endpoint := *resp.Properties
   154  
   155  	d.Set("name", resp.Name)
   156  	d.Set("endpoint_status", endpoint.EndpointStatus)
   157  	d.Set("target_resource_id", endpoint.TargetResourceID)
   158  	d.Set("target", endpoint.Target)
   159  	d.Set("weight", endpoint.Weight)
   160  	d.Set("priority", endpoint.Priority)
   161  	d.Set("endpoint_location", endpoint.EndpointLocation)
   162  	d.Set("endpoint_monitor_status", endpoint.EndpointMonitorStatus)
   163  	d.Set("min_child_endpoints", endpoint.MinChildEndpoints)
   164  
   165  	return nil
   166  }
   167  
   168  func resourceArmTrafficManagerEndpointDelete(d *schema.ResourceData, meta interface{}) error {
   169  	client := meta.(*ArmClient).trafficManagerEndpointsClient
   170  
   171  	id, err := parseAzureResourceID(d.Id())
   172  	if err != nil {
   173  		return err
   174  	}
   175  	resGroup := id.ResourceGroup
   176  	endpointType := d.Get("type").(string)
   177  	profileName := id.Path["trafficManagerProfiles"]
   178  
   179  	// endpoint name is keyed by endpoint type in ARM ID
   180  	name := id.Path[endpointType]
   181  
   182  	_, err = client.Delete(resGroup, profileName, endpointType, name)
   183  
   184  	return err
   185  }
   186  
   187  func getArmTrafficManagerEndpointProperties(d *schema.ResourceData) *trafficmanager.EndpointProperties {
   188  	var endpointProps trafficmanager.EndpointProperties
   189  
   190  	if targetResID := d.Get("target_resource_id").(string); targetResID != "" {
   191  		endpointProps.TargetResourceID = &targetResID
   192  	}
   193  
   194  	if target := d.Get("target").(string); target != "" {
   195  		endpointProps.Target = &target
   196  	}
   197  
   198  	if status := d.Get("endpoint_status").(string); status != "" {
   199  		endpointProps.EndpointStatus = &status
   200  	}
   201  
   202  	if weight := d.Get("weight").(int); weight != 0 {
   203  		w64 := int64(weight)
   204  		endpointProps.Weight = &w64
   205  	}
   206  
   207  	if priority := d.Get("priority").(int); priority != 0 {
   208  		p64 := int64(priority)
   209  		endpointProps.Priority = &p64
   210  	}
   211  
   212  	if location := d.Get("endpoint_location").(string); location != "" {
   213  		endpointProps.EndpointLocation = &location
   214  	}
   215  
   216  	if minChildEndpoints := d.Get("min_child_endpoints").(int); minChildEndpoints != 0 {
   217  		mci64 := int64(minChildEndpoints)
   218  		endpointProps.MinChildEndpoints = &mci64
   219  	}
   220  
   221  	return &endpointProps
   222  }
   223  
   224  func validateAzureRMTrafficManagerEndpointType(i interface{}, k string) (s []string, errors []error) {
   225  	valid := map[string]struct{}{
   226  		"azureEndpoints":    struct{}{},
   227  		"externalEndpoints": struct{}{},
   228  		"nestedEndpoints":   struct{}{},
   229  	}
   230  
   231  	if _, ok := valid[i.(string)]; !ok {
   232  		errors = append(errors, fmt.Errorf("endpoint type invalid, got %s", i.(string)))
   233  	}
   234  	return
   235  }
   236  
   237  func validateAzureRMTrafficManagerEndpointWeight(i interface{}, k string) (s []string, errors []error) {
   238  	w := i.(int)
   239  	if w < 1 || w > 1000 {
   240  		errors = append(errors, fmt.Errorf("endpoint weight must be between 1-1000 inclusive"))
   241  	}
   242  	return
   243  }
   244  
   245  func validateAzureRMTrafficManagerEndpointPriority(i interface{}, k string) (s []string, errors []error) {
   246  	p := i.(int)
   247  	if p < 1 || p > 1000 {
   248  		errors = append(errors, fmt.Errorf("endpoint priority must be between 1-1000 inclusive"))
   249  	}
   250  	return
   251  }