Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Resource:] azurerm_cdn_frontdoor_custom_domain_association - rework associations with azurerm_cdn_frontdoor_route #18600

Merged
merged 61 commits into from
Oct 14, 2022
Merged
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
46e2144
Initial check-in...
WodansSon Sep 30, 2022
fcb98e0
Churn while I work out the logic...
WodansSon Oct 1, 2022
bfb6235
So close...
WodansSon Oct 3, 2022
d17d5cc
Fix lint error...
WodansSon Oct 3, 2022
f8d3b0d
Fix case sensitivity and move toward association
WodansSon Oct 5, 2022
aa144c7
Merge branch 'main' into b_frontdoor_custom_domains
WodansSon Oct 5, 2022
9aaf01e
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
WodansSon Oct 6, 2022
6ad1250
Almost there... total redesign...
WodansSon Oct 6, 2022
3736a07
Done... now write test cases...
WodansSon Oct 7, 2022
a435800
Last of the case insensitive fix...
WodansSon Oct 7, 2022
f6bb268
Add nil checks to helper functions
WodansSon Oct 7, 2022
2ccf477
Fix race condition with route resource
WodansSon Oct 7, 2022
fc960fe
Add note to docs for route race condition
WodansSon Oct 8, 2022
1342d3c
Add third route to example
WodansSon Oct 8, 2022
4e6ee62
Fix lint error
WodansSon Oct 8, 2022
6113701
terrafmt
WodansSon Oct 8, 2022
f57c8a4
Fix up some loose ends and test cases
WodansSon Oct 9, 2022
e2bc53c
Fix comment spacing
WodansSon Oct 9, 2022
a64c107
Terrafmt test
WodansSon Oct 9, 2022
e46d4c7
Remove import check for association
WodansSon Oct 10, 2022
bed92ba
last minute changes
WodansSon Oct 10, 2022
9042993
Remove associate_with_cdn_frontdoor_route_id
WodansSon Oct 11, 2022
c3341da
remove validation from read func
WodansSon Oct 11, 2022
e128c68
refactor disable link resource
WodansSon Oct 12, 2022
2dc3833
Update nil error message
WodansSon Oct 12, 2022
87ef090
fix typo in var name
WodansSon Oct 12, 2022
fd01df8
Update website/docs/r/cdn_frontdoor_route_disable_link_to_default_dom…
WodansSon Oct 12, 2022
367c149
Update website/docs/r/cdn_frontdoor_route.html.markdown
WodansSon Oct 12, 2022
df9d442
Update website/docs/r/cdn_frontdoor_custom_domain_association.html.ma…
WodansSon Oct 12, 2022
84a11f9
Update website/docs/r/cdn_frontdoor_custom_domain_association.html.ma…
WodansSon Oct 12, 2022
ab0845e
Update internal/services/cdn/cdn_frontdoor_custom_domain_association_…
WodansSon Oct 13, 2022
a0d7881
Update internal/services/cdn/cdn_frontdoor_custom_domain_association_…
WodansSon Oct 13, 2022
88e1d0b
Update internal/services/cdn/cdn_frontdoor_custom_domain_resource.go
WodansSon Oct 13, 2022
2d8a224
Update internal/services/cdn/cdn_frontdoor_custom_domain_resource.go
WodansSon Oct 13, 2022
b9c1245
Update internal/services/cdn/cdn_frontdoor_custom_domain_resource.go
WodansSon Oct 13, 2022
d0a0cfe
Update internal/services/cdn/cdn_frontdoor_custom_domain_resource.go
WodansSon Oct 13, 2022
b48f8f6
Update internal/services/cdn/cdn_frontdoor_custom_domain_resource.go
WodansSon Oct 13, 2022
ec1f6aa
Update internal/services/cdn/cdn_frontdoor_endpoint_resource.go
WodansSon Oct 13, 2022
1665107
Update internal/services/cdn/cdn_frontdoor_endpoint_resource.go
WodansSon Oct 13, 2022
d387a0e
Update internal/services/cdn/cdn_frontdoor_endpoint_resource.go
WodansSon Oct 13, 2022
f789e03
Update internal/services/cdn/cdn_frontdoor_endpoint_resource.go
WodansSon Oct 13, 2022
dcae3cf
Update internal/services/cdn/cdn_frontdoor_firewall_policy_resource.go
WodansSon Oct 13, 2022
0ac6010
Update internal/services/cdn/cdn_frontdoor_firewall_policy_resource.go
WodansSon Oct 13, 2022
8a0779a
Update internal/services/cdn/cdn_frontdoor_helpers.go
WodansSon Oct 13, 2022
acb73d0
Update internal/services/cdn/cdn_frontdoor_helpers.go
WodansSon Oct 13, 2022
7b8b1bf
Update internal/services/cdn/cdn_frontdoor_helpers.go
WodansSon Oct 13, 2022
04caf16
Update internal/services/cdn/cdn_frontdoor_helpers.go
WodansSon Oct 13, 2022
954f74c
Update internal/services/cdn/cdn_frontdoor_helpers.go
WodansSon Oct 13, 2022
89cdf25
Update internal/services/cdn/cdn_frontdoor_helpers.go
WodansSon Oct 13, 2022
6f0bad3
Update internal/services/cdn/cdn_frontdoor_helpers.go
WodansSon Oct 13, 2022
3f87506
Update internal/services/cdn/cdn_frontdoor_origin_group_resource.go
WodansSon Oct 13, 2022
bf212b3
Update internal/services/cdn/cdn_frontdoor_origin_group_resource.go
WodansSon Oct 13, 2022
7c26b1c
Update internal/services/cdn/cdn_frontdoor_origin_group_resource.go
WodansSon Oct 13, 2022
c213b05
Update website/docs/r/cdn_frontdoor_custom_domain_association.html.ma…
WodansSon Oct 13, 2022
e18037e
remove all Insensitively from resource
WodansSon Oct 13, 2022
dc44953
Update website/docs/r/cdn_frontdoor_custom_domain_association.html.ma…
WodansSon Oct 13, 2022
1a281aa
Address PR comments
WodansSon Oct 13, 2022
6163ae6
Merge branch 'b_frontdoor_custom_domains' of https://github.com/hashi…
WodansSon Oct 13, 2022
ba54943
Fix frontmatter issue with doc
WodansSon Oct 13, 2022
fa1939f
Remove extra space from H1
WodansSon Oct 13, 2022
0572a38
remove friendly parse function
WodansSon Oct 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
So close...
WodansSon committed Oct 3, 2022
commit bfb62350afd8710ae3795b61d36cde8be5dae37b
229 changes: 107 additions & 122 deletions internal/services/cdn/cdn_frontdoor_custom_domain_resource.go
Original file line number Diff line number Diff line change
@@ -18,11 +18,6 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type RouteInfo struct {
CustomDomains []interface{}
Props *cdn.RouteProperties
}

func resourceCdnFrontDoorCustomDomain() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceCdnFrontDoorCustomDomainCreate,
@@ -69,7 +64,7 @@ func resourceCdnFrontDoorCustomDomain() *pluginsdk.Resource {
Computed: !features.FourPointOhBeta(),
Deprecated: "'associate_with_cdn_frontdoor_route_id' has been deprecated in favour of 'associate_with_cdn_frontdoor_route_ids' and will be removed in version 4.0 of the AzureRM provider",
ConflictsWith: func() []string {
if !features.FourPointOh() {
if !features.FourPointOhBeta() {
return []string{"associate_with_cdn_frontdoor_route_ids"}
}
return []string{}
@@ -82,7 +77,7 @@ func resourceCdnFrontDoorCustomDomain() *pluginsdk.Resource {
Optional: true,
Computed: !features.FourPointOhBeta(),
ConflictsWith: func() []string {
if !features.FourPointOh() {
if !features.FourPointOhBeta() {
return []string{"associate_with_cdn_frontdoor_route_id"}
}
return []string{}
@@ -182,7 +177,23 @@ func resourceCdnFrontDoorCustomDomainCreate(d *pluginsdk.ResourceData, meta inte
return tf.ImportAsExistsError("azurerm_cdn_frontdoor_custom_domain", id.ID())
}

// preValidatedDomain := d.Get("pre_validated_custom_domain_id").(string)
// do the validation first on the deprecated field...
if !features.FourPointOhBeta() {
if route := d.Get("associate_with_cdn_frontdoor_route_id").(string); route != "" {
routes := []interface{}{route}
if err := validateCustomDomainRoutesProfileAndEndpoints(routes, &id); err != nil {
return err
}
}
}

// do the validation on the 4.0 field...
if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) != 0 {
if err := validateCustomDomainRoutesProfileAndEndpoints(routes, &id); err != nil {
return err
}
}

dnsZone := d.Get("dns_zone_id").(string)
tls := d.Get("tls").([]interface{})

@@ -216,35 +227,25 @@ func resourceCdnFrontDoorCustomDomainCreate(d *pluginsdk.ResourceData, meta inte

// Associate the custom domain with the route, if that field was passed...
if !features.FourPointOhBeta() {
if routes := []interface{}{d.Get("associate_with_cdn_frontdoor_route_id")}; len(routes) > 0 {
if err := validateCustomDomainRoutesProfile(routes, &id); err != nil {
return err
}
if route := d.Get("associate_with_cdn_frontdoor_route_id").(string); route != "" {
routes := []interface{}{route}

if err = addCustomDomainAssociationToRoutes(d, meta, routes, &id); err != nil {
return err
} else {
d.Set("associate_with_cdn_frontdoor_route_id", route)
}

d.Set("associate_with_cdn_frontdoor_route_id", routes[0].(string))
} else {
d.Set("associate_with_cdn_frontdoor_route_id", "")
}
}

if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) > 0 {
if err := hasDuplicateRoute(routes); err != nil {
return err
}

if err := validateCustomDomainRoutesProfile(routes, &id); err != nil {
return err
}

// NOTE: To associate a custom domain with more than one route, you must also make sure that all of the routes point to the same endpoint
// NOTE: I also now have to wait until all of the front door custom domains are created befor I move on here?
if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) != 0 {
if err = addCustomDomainAssociationToRoutes(d, meta, routes, &id); err != nil {
return err
} else {
d.Set("associate_with_cdn_frontdoor_route_ids", routes)
}

d.Set("associate_with_cdn_frontdoor_route_ids", routes)
}

return resourceCdnFrontDoorCustomDomainRead(d, meta)
@@ -289,6 +290,16 @@ func resourceCdnFrontDoorCustomDomainRead(d *pluginsdk.ResourceData, meta interf
}
}

if !features.FourPointOhBeta() {
if route := d.Get("associate_with_cdn_frontdoor_route_id").(string); route == "" {
d.Set("associate_with_cdn_frontdoor_route_id", "")
}
}

if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) == 0 {
d.Set("associate_with_cdn_frontdoor_route_ids", []interface{}{})
}

return nil
}

@@ -302,17 +313,19 @@ func resourceCdnFrontDoorCustomDomainUpdate(d *pluginsdk.ResourceData, meta inte
return err
}

// validate the associations first...
// validate the associations first for common errors...
if !features.FourPointOhBeta() {
if routes := []interface{}{d.Get("associate_with_cdn_frontdoor_route_id")}; len(routes) > 0 {
if err := validateCustomDomainRoutesProfile(routes, id); err != nil {
return err
if route := d.Get("associate_with_cdn_frontdoor_route_id").(string); route != "" {
routes := []interface{}{route}

if err := validateCustomDomainRoutesProfileAndEndpoints(routes, id); err != nil {
return fmt.Errorf("azurerm_cdn_frontdoor_custom_domain: the 'associate_with_cdn_frontdoor_route_id' field is invalid: %+v", err)
}
}
}

if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) > 0 {
if err := validateCustomDomainRoutesProfile(routes, id); err != nil {
if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) != 0 {
if err := validateCustomDomainRoutesProfileAndEndpoints(routes, id); err != nil {
return fmt.Errorf("azurerm_cdn_frontdoor_custom_domain: the 'associate_with_cdn_frontdoor_route_ids' field is invalid: %+v", err)
}
}
@@ -368,116 +381,90 @@ func resourceCdnFrontDoorCustomDomainUpdate(d *pluginsdk.ResourceData, meta inte

// Now that the Custom Domain has been updated we need to ensure the referential integrity of the route resource
// and associate/unassociate the custom domain with the route(s), if that field was defined/removed...

if !features.FourPointOhBeta() {
if d.HasChange("associate_with_cdn_frontdoor_route_id") {
old, new := d.GetChange("associate_with_cdn_frontdoor_route_id")
oldRouteValue := old.(string)
newRouteValue := new.(string)
if route := d.Get("associate_with_cdn_frontdoor_route_id").(string); route != "" {
if d.HasChange("associate_with_cdn_frontdoor_route_id") {
old, new := d.GetChange("associate_with_cdn_frontdoor_route_id")
oldRoute := []interface{}{old.(string)}
newRoute := []interface{}{new.(string)}

switch {
case (len(oldRoute) == 0 && len(newRoute) != 0):
// Add
if err := addCustomDomainAssociationToRoutes(d, meta, newRoute, id); err != nil {
return err
}

switch {
case (oldRouteValue == "" && newRouteValue != ""):
// Add
routes := []interface{}{oldRouteValue}
d.Set("associate_with_cdn_frontdoor_route_id", new.(string))

// add the association to the new route...
if err := addCustomDomainAssociationToRoutes(d, meta, routes, id); err != nil {
return err
}
case (len(oldRoute) != 0 && len(newRoute) == 0):
// Remove
if err := removeCustomDomainAssociationFromRoutes(d, meta, newRoute, id); err != nil {
return err
}

case (oldRouteValue != "" && newRouteValue == ""):
// Remove
routes := []interface{}{oldRouteValue}
d.Set("associate_with_cdn_frontdoor_route_id", "")

// remove the association from the old route..
if err := removeCustomDomainAssociationFromRoutes(d, meta, routes, id); err != nil {
return err
}
case (len(oldRoute) != 0 && len(newRoute) != 0 && !strings.EqualFold(old.(string), new.(string))):
// Swap
if err = removeCustomDomainAssociationFromRoutes(d, meta, oldRoute, id); err != nil {
return err
}

d.Set("associate_with_cdn_frontdoor_route_id", "")
d.Set("associate_with_cdn_frontdoor_route_id", "")

case (oldRouteValue != "" && newRouteValue != "" && !strings.EqualFold(oldRouteValue, newRouteValue)):
// Swap
routes := []interface{}{oldRouteValue}
// add the association to the new route...
if err = addCustomDomainAssociationToRoutes(d, meta, newRoute, id); err != nil {
return err
}

// remove the association from the old route..
if err = removeCustomDomainAssociationFromRoutes(d, meta, routes, id); err != nil {
return err
d.Set("associate_with_cdn_frontdoor_route_id", new.(string))
}
}
}
}

d.Set("associate_with_cdn_frontdoor_route_id", "")
routes = []interface{}{newRouteValue}
if d.HasChange("associate_with_cdn_frontdoor_route_ids") {
if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) != 0 {
old, new := d.GetChange("associate_with_cdn_frontdoor_route_ids")
oldRoutes := old.([]interface{})
newRoutes := new.([]interface{})

// add the association to the new route...
if err = addCustomDomainAssociationToRoutes(d, meta, routes, id); err != nil {
switch {
case (len(oldRoutes) == 0 && len(newRoutes) != 0):
// Add
if err = addCustomDomainAssociationToRoutes(d, meta, newRoutes, id); err != nil {
return err
}

d.Set("associate_with_cdn_frontdoor_route_id", newRouteValue)
}
}
}
d.Set("associate_with_cdn_frontdoor_route_ids", newRoutes)

if d.HasChange("associate_with_cdn_frontdoor_route_ids") {
old, new := d.GetChange("associate_with_cdn_frontdoor_route_ids")
oldRoutes := old.([]interface{})
newRoutes := new.([]interface{})

switch {
case (len(oldRoutes) == 0 && len(newRoutes) > 0):
// Add
err = addCustomDomainAssociationToRoutes(d, meta, newRoutes, id)
if err != nil {
return err
}
case (len(oldRoutes) > 0 && len(newRoutes) == 0):
// Remove the custom domain from all of the previously associated routes...
err = removeCustomDomainAssociationFromRoutes(d, meta, oldRoutes, id)
if err != nil {
return err
}
case (len(oldRoutes) != 0 && len(newRoutes) == 0):
// Remove
if err = removeCustomDomainAssociationFromRoutes(d, meta, oldRoutes, id); err != nil {
return err
}

d.Set("associate_with_cdn_frontdoor_route_ids", []interface{}{})

d.Set("associate_with_cdn_frontdoor_route_ids", newRoutes)

case (len(oldRoutes) > 0 && len(newRoutes) > 0):
// Remove(e.g. Swap): here I need to find out what was in the old list and is not in the new list,
// those are the routes I need to remove this custom domain from...

// find the delta between the old and the new lists...
var removeDelta []interface{}
for _, nr := range newRoutes {
var found bool
var removeRoute interface{}
for _, or := range oldRoutes {
removeRoute = or
if strings.EqualFold(or.(string), nr.(string)) {
// they are in both lists so ignore it...
found = true
break
case (len(oldRoutes) != 0 && len(newRoutes) != 0):
// Swap
if removeRoutes, sharedRoutes := getRemoveRoutesDelta(oldRoutes, newRoutes); len(removeRoutes) != 0 {
if err = removeCustomDomainAssociationFromRoutes(d, meta, removeRoutes, id); err != nil {
return err
}
}

// I did not find a route in the new list that is in the old route list so I need to disassociate that route with this custom domain...
if !found {
removeDelta = append(removeDelta, removeRoute)
d.Set("associate_with_cdn_frontdoor_route_ids", sharedRoutes)
}
}

// delta now contains the list of all the routes that were not in our new list of routes that were in the old list of routes...
if len(removeDelta) > 0 {
err = removeCustomDomainAssociationFromRoutes(d, meta, removeDelta, id)
if err != nil {
if err = addCustomDomainAssociationToRoutes(d, meta, newRoutes, id); err != nil {
return err
}
}

// do not need to do the add delta as the addCustomDomainAssociationToRoutes figures that out for us automatically...
err = addCustomDomainAssociationToRoutes(d, meta, newRoutes, id)
if err != nil {
return err
d.Set("associate_with_cdn_frontdoor_route_ids", newRoutes)
}
}

d.Set("associate_with_cdn_frontdoor_route_ids", newRoutes)
}

return resourceCdnFrontDoorCustomDomainRead(d, meta)
@@ -496,18 +483,16 @@ func resourceCdnFrontDoorCustomDomainDelete(d *pluginsdk.ResourceData, meta inte
// NOTE: If the custom domain is still associated with a route you cannot delete it
// you must first update the route to remove the association with the custom domain...
if !features.FourPointOhBeta() {
if route := []interface{}{d.Get("associate_with_cdn_frontdoor_route_id")}; len(route) > 0 {
if route := d.Get("associate_with_cdn_frontdoor_route_id").(string); route != "" {
routes := []interface{}{route}

// remove the association from the old route..
if err := removeCustomDomainAssociationFromRoutes(d, meta, routes, id); err != nil {
return err
}
}
}

routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{})
if len(routes) > 0 {
if routes := d.Get("associate_with_cdn_frontdoor_route_ids").([]interface{}); len(routes) != 0 {
// remove the association from the old route..
if err := removeCustomDomainAssociationFromRoutes(d, meta, routes, id); err != nil {
return err
Loading