-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add IAP support for backend services #471
Changes from 4 commits
ae5ad95
79df895
7b02cdb
b67c08e
e865ea2
285b10b
9de4d86
ec4afb2
95bb1c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,32 @@ func resourceComputeBackendService() *schema.Resource { | |
MaxItems: 1, | ||
}, | ||
|
||
"iap": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Elem: &schema.Resource{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of our other resources have Elem as the last item in the struct. I'm not sure why this resource does it the other way but it would be awesome if you could move this one and change some of the others in this file too! |
||
Schema: map[string]*schema.Schema{ | ||
"enabled": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether we should make this Required, that way people don't accidentally try to enable it by putting in the block but forgetting this field There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered this as well. I think about how I work, and commenting out IAP enabled would "disable" it, without having to edit the line and set it as false. What's the standard here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something else to consider here, should we even have the enabled flag? Should it be implicit by providing the keys? |
||
Default: false, | ||
}, | ||
"oauth2_client_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "", | ||
}, | ||
"oauth2_client_secret": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "", | ||
}, | ||
}, | ||
}, | ||
Optional: true, | ||
MaxItems: 1, | ||
Set: func(i interface{}) int { return 0 }, | ||
}, | ||
|
||
"backend": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Elem: &schema.Resource{ | ||
|
@@ -203,6 +229,7 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) | |
d.Set("self_link", service.SelfLink) | ||
d.Set("backend", flattenBackends(service.Backends)) | ||
d.Set("connection_draining_timeout_sec", service.ConnectionDraining.DrainingTimeoutSec) | ||
d.Set("iap", flattenIap(service.Iap)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will service.Iap ever be nil? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only in-so-far as the service object doesn't initialize it a few lines up from here. I'll add a check in the flatten function. |
||
|
||
d.Set("health_checks", service.HealthChecks) | ||
|
||
|
@@ -261,6 +288,27 @@ func resourceComputeBackendServiceDelete(d *schema.ResourceData, meta interface{ | |
return nil | ||
} | ||
|
||
func expandIap(configured []interface{}) *compute.BackendServiceIAP { | ||
data := configured[0].(map[string]interface{}) | ||
iap := &compute.BackendServiceIAP{ | ||
Enabled: data["enabled"].(bool), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without also setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will double check and also add tests. |
||
Oauth2ClientId: data["oauth2_client_id"].(string), | ||
Oauth2ClientSecret: data["oauth2_client_secret"].(string), | ||
} | ||
return iap | ||
} | ||
|
||
func flattenIap(iap *compute.BackendServiceIAP) []map[string]interface{} { | ||
result := make([]map[string]interface{}, 0, 1) | ||
iapMap := map[string]interface{}{ | ||
"enabled": iap.Enabled, | ||
"oauth2_client_id": iap.Oauth2ClientId, | ||
"oauth2_client_secret": iap.Oauth2ClientSecret, | ||
} | ||
result = append(result, iapMap) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally up to you, but a shorter way to do this would be:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hadn't considered that -- I agree, much cleaner. |
||
return result | ||
} | ||
|
||
func expandBackends(configured []interface{}) []*compute.Backend { | ||
backends := make([]*compute.Backend, 0, len(configured)) | ||
|
||
|
@@ -309,7 +357,6 @@ func flattenBackends(backends []*compute.Backend) []map[string]interface{} { | |
data["max_rate"] = b.MaxRate | ||
data["max_rate_per_instance"] = b.MaxRatePerInstance | ||
data["max_utilization"] = b.MaxUtilization | ||
|
||
result = append(result, data) | ||
} | ||
|
||
|
@@ -328,6 +375,10 @@ func expandBackendService(d *schema.ResourceData) compute.BackendService { | |
HealthChecks: healthChecks, | ||
} | ||
|
||
if v, ok := d.GetOk("iap"); ok { | ||
service.Iap = expandIap(v.(*schema.Set).List()) | ||
} | ||
|
||
if v, ok := d.GetOk("backend"); ok { | ||
service.Backends = expandBackends(v.(*schema.Set).List()) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's MaxItems: 1 anyway, I'd recommend just making this a list. Then you can also get rid of the Set fn on line 63