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

Add new Config entity to Identitytoolkit #12665

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6587.txt
@@ -0,0 +1,3 @@
```release-note:new-resource
`google_identity_platform_config`
```
5 changes: 3 additions & 2 deletions google/provider.go
Expand Up @@ -890,9 +890,9 @@ func Provider() *schema.Provider {
return provider
}

// Generated resources: 234
// Generated resources: 235
// Generated IAM resources: 138
// Total generated resources: 372
// Total generated resources: 373
func ResourceMap() map[string]*schema.Resource {
resourceMap, _ := ResourceMapWithErrors()
return resourceMap
Expand Down Expand Up @@ -1166,6 +1166,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_iap_tunnel_iam_policy": ResourceIamPolicy(IapTunnelIamSchema, IapTunnelIamUpdaterProducer, IapTunnelIdParseFunc),
"google_iap_brand": resourceIapBrand(),
"google_iap_client": resourceIapClient(),
"google_identity_platform_config": resourceIdentityPlatformConfig(),
"google_identity_platform_default_supported_idp_config": resourceIdentityPlatformDefaultSupportedIdpConfig(),
"google_identity_platform_tenant_default_supported_idp_config": resourceIdentityPlatformTenantDefaultSupportedIdpConfig(),
"google_identity_platform_inbound_saml_config": resourceIdentityPlatformInboundSamlConfig(),
Expand Down
260 changes: 260 additions & 0 deletions google/resource_identity_platform_config.go
@@ -0,0 +1,260 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import (
"fmt"
"log"
"reflect"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceIdentityPlatformConfig() *schema.Resource {
return &schema.Resource{
Create: resourceIdentityPlatformConfigCreate,
Read: resourceIdentityPlatformConfigRead,
Update: resourceIdentityPlatformConfigUpdate,
Delete: resourceIdentityPlatformConfigDelete,

Importer: &schema.ResourceImporter{
State: resourceIdentityPlatformConfigImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},

Schema: map[string]*schema.Schema{
"autodelete_anonymous_users": {
Type: schema.TypeBool,
Optional: true,
Description: `Whether anonymous users will be auto-deleted after a period of 30 days`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the Config resource`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
UseJSONNumber: true,
}
}

func resourceIdentityPlatformConfigCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

obj := make(map[string]interface{})
autodeleteAnonymousUsersProp, err := expandIdentityPlatformConfigAutodeleteAnonymousUsers(d.Get("autodelete_anonymous_users"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("autodelete_anonymous_users"); !isEmptyValue(reflect.ValueOf(autodeleteAnonymousUsersProp)) && (ok || !reflect.DeepEqual(v, autodeleteAnonymousUsersProp)) {
obj["autodeleteAnonymousUsers"] = autodeleteAnonymousUsersProp
}

url, err := replaceVars(d, config, "{{IdentityPlatformBasePath}}projects/{{project}}/identityPlatform:initializeAuth")
if err != nil {
return err
}

log.Printf("[DEBUG] Creating new Config: %#v", obj)
billingProject := ""

project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for Config: %s", err)
}
billingProject = project

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating Config: %s", err)
}
if err := d.Set("name", flattenIdentityPlatformConfigName(res["name"], d, config)); err != nil {
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
}

// Store the ID now
id, err := replaceVars(d, config, "projects/{{project}}/config")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

log.Printf("[DEBUG] Finished creating Config %q: %#v", d.Id(), res)

return resourceIdentityPlatformConfigRead(d, meta)
}

func resourceIdentityPlatformConfigRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{IdentityPlatformBasePath}}projects/{{project}}/config")
if err != nil {
return err
}

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for Config: %s", err)
}
billingProject = project

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("IdentityPlatformConfig %q", d.Id()))
}

if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading Config: %s", err)
}

if err := d.Set("name", flattenIdentityPlatformConfigName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading Config: %s", err)
}
if err := d.Set("autodelete_anonymous_users", flattenIdentityPlatformConfigAutodeleteAnonymousUsers(res["autodeleteAnonymousUsers"], d, config)); err != nil {
return fmt.Errorf("Error reading Config: %s", err)
}

return nil
}

func resourceIdentityPlatformConfigUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

billingProject := ""

project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for Config: %s", err)
}
billingProject = project

obj := make(map[string]interface{})
autodeleteAnonymousUsersProp, err := expandIdentityPlatformConfigAutodeleteAnonymousUsers(d.Get("autodelete_anonymous_users"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("autodelete_anonymous_users"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, autodeleteAnonymousUsersProp)) {
obj["autodeleteAnonymousUsers"] = autodeleteAnonymousUsersProp
}

url, err := replaceVars(d, config, "{{IdentityPlatformBasePath}}projects/{{project}}/config")
if err != nil {
return err
}

log.Printf("[DEBUG] Updating Config %q: %#v", d.Id(), obj)
updateMask := []string{}

if d.HasChange("autodelete_anonymous_users") {
updateMask = append(updateMask, "autodeleteAnonymousUsers")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}

// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}

res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return fmt.Errorf("Error updating Config %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating Config %q: %#v", d.Id(), res)
}

return resourceIdentityPlatformConfigRead(d, meta)
}

func resourceIdentityPlatformConfigDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[WARNING] IdentityPlatform Config resources"+
" cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+
" state, but will still be present on Google Cloud.", d.Id())
d.SetId("")

return nil
}

func resourceIdentityPlatformConfigImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/config",
"projects/(?P<project>[^/]+)",
"(?P<project>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "projects/{{project}}/config")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}

func flattenIdentityPlatformConfigName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenIdentityPlatformConfigAutodeleteAnonymousUsers(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandIdentityPlatformConfigAutodeleteAnonymousUsers(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
69 changes: 69 additions & 0 deletions google/resource_identity_platform_config_generated_test.go
@@ -0,0 +1,69 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccIdentityPlatformConfig_identityPlatformConfigBasicExample(t *testing.T) {
skipIfVcr(t)
t.Parallel()

context := map[string]interface{}{
"org_id": getTestOrgFromEnv(t),
"billing_acct": getTestBillingAccountFromEnv(t),
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccIdentityPlatformConfig_identityPlatformConfigBasicExample(context),
},
{
ResourceName: "google_identity_platform_config.default",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccIdentityPlatformConfig_identityPlatformConfigBasicExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_project" "default" {
project_id = "tf-test%{random_suffix}"
name = "tf-test%{random_suffix}"
org_id = "%{org_id}"
billing_account = "%{billing_acct}"
}

resource "google_project_service" "apigee" {
project = google_project.project.project_id
service = "identitytoolkit.googleapis.com"
}


resource "google_identity_platform_config" "default" {
project = google_project.default.project_id
autodelete_anonymous_users = true
}
`, context)
}