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