github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 err != nil { 156 return fmt.Errorf("Error making Read request on TrafficManager Endpoint %s: %s", name, err) 157 } 158 if resp.StatusCode == http.StatusNotFound { 159 d.SetId("") 160 return nil 161 } 162 163 endpoint := *resp.Properties 164 165 d.Set("name", resp.Name) 166 d.Set("type", endpointType) 167 d.Set("profile_name", profileName) 168 d.Set("endpoint_status", endpoint.EndpointStatus) 169 d.Set("target_resource_id", endpoint.TargetResourceID) 170 d.Set("target", endpoint.Target) 171 d.Set("weight", endpoint.Weight) 172 d.Set("priority", endpoint.Priority) 173 d.Set("endpoint_location", endpoint.EndpointLocation) 174 d.Set("endpoint_monitor_status", endpoint.EndpointMonitorStatus) 175 d.Set("min_child_endpoints", endpoint.MinChildEndpoints) 176 177 return nil 178 } 179 180 func resourceArmTrafficManagerEndpointDelete(d *schema.ResourceData, meta interface{}) error { 181 client := meta.(*ArmClient).trafficManagerEndpointsClient 182 183 id, err := parseAzureResourceID(d.Id()) 184 if err != nil { 185 return err 186 } 187 resGroup := id.ResourceGroup 188 endpointType := d.Get("type").(string) 189 profileName := id.Path["trafficManagerProfiles"] 190 191 // endpoint name is keyed by endpoint type in ARM ID 192 name := id.Path[endpointType] 193 194 _, err = client.Delete(resGroup, profileName, endpointType, name) 195 196 return err 197 } 198 199 func getArmTrafficManagerEndpointProperties(d *schema.ResourceData) *trafficmanager.EndpointProperties { 200 var endpointProps trafficmanager.EndpointProperties 201 202 if targetResID := d.Get("target_resource_id").(string); targetResID != "" { 203 endpointProps.TargetResourceID = &targetResID 204 } 205 206 if target := d.Get("target").(string); target != "" { 207 endpointProps.Target = &target 208 } 209 210 if status := d.Get("endpoint_status").(string); status != "" { 211 endpointProps.EndpointStatus = &status 212 } 213 214 if weight := d.Get("weight").(int); weight != 0 { 215 w64 := int64(weight) 216 endpointProps.Weight = &w64 217 } 218 219 if priority := d.Get("priority").(int); priority != 0 { 220 p64 := int64(priority) 221 endpointProps.Priority = &p64 222 } 223 224 if location := d.Get("endpoint_location").(string); location != "" { 225 endpointProps.EndpointLocation = &location 226 } 227 228 if minChildEndpoints := d.Get("min_child_endpoints").(int); minChildEndpoints != 0 { 229 mci64 := int64(minChildEndpoints) 230 endpointProps.MinChildEndpoints = &mci64 231 } 232 233 return &endpointProps 234 } 235 236 func validateAzureRMTrafficManagerEndpointType(i interface{}, k string) (s []string, errors []error) { 237 valid := map[string]struct{}{ 238 "azureEndpoints": struct{}{}, 239 "externalEndpoints": struct{}{}, 240 "nestedEndpoints": struct{}{}, 241 } 242 243 if _, ok := valid[i.(string)]; !ok { 244 errors = append(errors, fmt.Errorf("endpoint type invalid, got %s", i.(string))) 245 } 246 return 247 } 248 249 func validateAzureRMTrafficManagerEndpointWeight(i interface{}, k string) (s []string, errors []error) { 250 w := i.(int) 251 if w < 1 || w > 1000 { 252 errors = append(errors, fmt.Errorf("endpoint weight must be between 1-1000 inclusive")) 253 } 254 return 255 } 256 257 func validateAzureRMTrafficManagerEndpointPriority(i interface{}, k string) (s []string, errors []error) { 258 p := i.(int) 259 if p < 1 || p > 1000 { 260 errors = append(errors, fmt.Errorf("endpoint priority must be between 1-1000 inclusive")) 261 } 262 return 263 }