From 0f155acd76ddb924197a6363c344f66a8decfc5f Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 21 Oct 2022 10:52:35 -0700 Subject: [PATCH] Add AndroidApp to Firebase. (#6629) (#12855) Signed-off-by: Modular Magician Signed-off-by: Modular Magician --- .changelog/6629.txt | 3 + ...ource_firebase_android_app_sweeper_test.go | 119 ++++++++++++++++++ ...source_firebase_android_app_update_test.go | 1 + .../docs/r/firebase_android_app.html.markdown | 112 +++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100644 .changelog/6629.txt create mode 100644 google/resource_firebase_android_app_sweeper_test.go create mode 100644 google/resource_firebase_android_app_update_test.go create mode 100644 website/docs/r/firebase_android_app.html.markdown diff --git a/.changelog/6629.txt b/.changelog/6629.txt new file mode 100644 index 00000000000..e3972ecc597 --- /dev/null +++ b/.changelog/6629.txt @@ -0,0 +1,3 @@ +```release-note:new-resource + +``` diff --git a/google/resource_firebase_android_app_sweeper_test.go b/google/resource_firebase_android_app_sweeper_test.go new file mode 100644 index 00000000000..e7ca39cca71 --- /dev/null +++ b/google/resource_firebase_android_app_sweeper_test.go @@ -0,0 +1,119 @@ +// ---------------------------------------------------------------------------- +// +// *** HANDWRITTEN CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "context" + "log" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func init() { + resource.AddTestSweepers("FirebaseAndroidApp", &resource.Sweeper{ + Name: "FirebaseAndroidApp", + F: testSweepFirebaseAndroidApp, + }) +} + +// At the time of writing, the CI only passes us-central1 as the region +func testSweepFirebaseAndroidApp(region string) error { + resourceName := "FirebaseAndroidApp" + log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) + + config, err := sharedConfigForRegion(region) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) + return err + } + + err = config.LoadAndValidate(context.Background()) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) + return err + } + + t := &testing.T{} + billingId := getTestBillingAccountFromEnv(t) + + // Setup variables to replace in list template + d := &ResourceDataMock{ + FieldsInSchema: map[string]interface{}{ + "project": config.Project, + "region": region, + "location": region, + "zone": "-", + "billing_account": billingId, + }, + } + + listTemplate := strings.Split("https://firebase.googleapis.com/v1beta1/projects/{{project}}/androidApps", "?")[0] + listUrl, err := replaceVars(d, config, listTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) + return nil + } + + res, err := sendRequest(config, "GET", config.Project, listUrl, config.userAgent, nil) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) + return nil + } + + resourceList, ok := res["androidApps"] + if !ok { + log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") + return nil + } + + rl := resourceList.([]interface{}) + + log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) + // Keep count of items that aren't sweepable for logging. + nonPrefixCount := 0 + for _, ri := range rl { + obj := ri.(map[string]interface{}) + if obj["name"] == nil { + log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) + return nil + } + + name := GetResourceNameFromSelfLink(obj["name"].(string)) + // Skip resources that shouldn't be sweeped + if !isSweepableTestResource(name) { + nonPrefixCount++ + continue + } + + deleteTemplate := "https://firebase.googleapis.com/v1beta1/{{name}}:remove" + deleteUrl, err := replaceVars(d, config, deleteTemplate) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) + return nil + } + deleteUrl = deleteUrl + name + + body := make(map[string]interface{}) + body["immediate"] = true + + // Don't wait on operations as we may have a lot to delete + _, err = sendRequest(config, "DELETE", config.Project, deleteUrl, config.userAgent, body) + if err != nil { + log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) + } else { + log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) + } + } + + if nonPrefixCount > 0 { + log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) + } + + return nil +} diff --git a/google/resource_firebase_android_app_update_test.go b/google/resource_firebase_android_app_update_test.go new file mode 100644 index 00000000000..71664db3c87 --- /dev/null +++ b/google/resource_firebase_android_app_update_test.go @@ -0,0 +1 @@ +package google diff --git a/website/docs/r/firebase_android_app.html.markdown b/website/docs/r/firebase_android_app.html.markdown new file mode 100644 index 00000000000..9f453eb9443 --- /dev/null +++ b/website/docs/r/firebase_android_app.html.markdown @@ -0,0 +1,112 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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. +# +# ---------------------------------------------------------------------------- +subcategory: "Firebase" +page_title: "Google: google_firebase_android_app" +description: |- + A Google Cloud Firebase Android application instance +--- + +# google\_firebase\_android\_app + +A Google Cloud Firebase Android application instance + +~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider. +See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources. + +To get more information about AndroidApp, see: + +* [API documentation](https://firebase.google.com/docs/projects/api/reference/rest/v1beta1/projects.androidApps) +* How-to Guides + * [Official Documentation](https://firebase.google.com/docs/android/setup) + +## Example Usage - Firebase Android App Basic + + +```hcl +resource "google_firebase_android_app" "basic" { + provider = google-beta + project = "my-project-name" + display_name = "Display Name Basic" + package_name = "" +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `display_name` - + (Required) + The user-assigned display name of the App. + + +- - - + + +* `package_name` - + (Optional) + Immutable. The canonical package name of the Android app as would appear in the Google Play + Developer Console. + +* `deletion_policy` - + (Optional) + (Optional) Set to `ABANDON` to allow the AndroidApp to be untracked from terraform state + rather than deleted upon `terraform destroy`. This is useful because the AndroidApp may be + serving traffic. Set to `DELETE` to delete the AndroidApp. Default to `DELETE`. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + +* `id` - an identifier for the resource with format `{{name}}` + +* `name` - + The fully qualified resource name of the App, for example: + projects/projectId/androidApps/appId + +* `app_id` - + Immutable. The globally unique, Firebase-assigned identifier of the App. + This identifier should be treated as an opaque token, as the data format is not specified. + + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 20 minutes. +- `update` - Default is 20 minutes. +- `delete` - Default is 20 minutes. + +## Import + + +AndroidApp can be imported using any of these accepted formats: + +``` +$ terraform import google_firebase_android_app.default projects/{{project}}/androidApps/{{appId}} +$ terraform import google_firebase_android_app.default {{project}}/{{appId}} +$ terraform import google_firebase_android_app.default androidApps/{{appId}} +$ terraform import google_firebase_android_app.default {{appId}} +``` + +## User Project Overrides + +This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/guides/provider_reference.html#user_project_override).