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