-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add resource for enabling firebase * removing whitespace for GA terraform * Change skip_delete to resource level property Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
61737b6
commit c1eeb70
Showing
8 changed files
with
435 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
firebase: Add the `google_firebase_project` resource which will enable Firebase for a referenced Google project. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// 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 ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type FirebaseOperationWaiter struct { | ||
Config *Config | ||
Project string | ||
CommonOperationWaiter | ||
} | ||
|
||
func (w *FirebaseOperationWaiter) QueryOp() (interface{}, error) { | ||
if w == nil { | ||
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.") | ||
} | ||
// Returns the proper get. | ||
url := fmt.Sprintf("https://firebase.googleapis.com/v1beta1/%s", w.CommonOperationWaiter.Op.Name) | ||
return sendRequest(w.Config, "GET", w.Project, url, nil) | ||
} | ||
|
||
func createFirebaseWaiter(config *Config, op map[string]interface{}, project, activity string) (*FirebaseOperationWaiter, error) { | ||
if val, ok := op["name"]; !ok || val == "" { | ||
// This was a synchronous call - there is no operation to wait for. | ||
return nil, nil | ||
} | ||
w := &FirebaseOperationWaiter{ | ||
Config: config, | ||
Project: project, | ||
} | ||
if err := w.CommonOperationWaiter.SetOp(op); err != nil { | ||
return nil, err | ||
} | ||
return w, nil | ||
} | ||
|
||
// nolint: deadcode,unused | ||
func firebaseOperationWaitTimeWithResponse(config *Config, op map[string]interface{}, response *map[string]interface{}, project, activity string, timeoutMinutes int) error { | ||
w, err := createFirebaseWaiter(config, op, project, activity) | ||
if err != nil || w == nil { | ||
// If w is nil, the op was synchronous. | ||
return err | ||
} | ||
if err := OperationWait(w, activity, timeoutMinutes, config.PollInterval); err != nil { | ||
return err | ||
} | ||
return json.Unmarshal([]byte(w.CommonOperationWaiter.Op.Response), response) | ||
} | ||
|
||
func firebaseOperationWaitTime(config *Config, op map[string]interface{}, project, activity string, timeoutMinutes int) error { | ||
w, err := createFirebaseWaiter(config, op, project, activity) | ||
if err != nil || w == nil { | ||
// If w is nil, the op was synchronous. | ||
return err | ||
} | ||
return OperationWait(w, activity, timeoutMinutes, config.PollInterval) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// 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" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceFirebaseProject() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceFirebaseProjectCreate, | ||
Read: resourceFirebaseProjectRead, | ||
Delete: resourceFirebaseProjectDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: resourceFirebaseProjectImport, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(10 * time.Minute), | ||
Delete: schema.DefaultTimeout(10 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"display_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The GCP project display name`, | ||
}, | ||
"project_number": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The number of the google project that firebase is enabled on.`, | ||
}, | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceFirebaseProjectCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
obj := make(map[string]interface{}) | ||
|
||
url, err := replaceVars(d, config, "{{FirebaseBasePath}}projects/{{project}}:addFirebase") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Creating new Project: %#v", obj) | ||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := sendRequestWithTimeout(config, "POST", project, url, obj, d.Timeout(schema.TimeoutCreate)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating Project: %s", err) | ||
} | ||
|
||
// Store the ID now | ||
id, err := replaceVars(d, config, "projects/{{project}}/{{name}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
err = firebaseOperationWaitTime( | ||
config, res, project, "Creating Project", | ||
int(d.Timeout(schema.TimeoutCreate).Minutes())) | ||
|
||
if err != nil { | ||
// The resource didn't actually create | ||
d.SetId("") | ||
return fmt.Errorf("Error waiting to create Project: %s", err) | ||
} | ||
|
||
log.Printf("[DEBUG] Finished creating Project %q: %#v", d.Id(), res) | ||
|
||
return resourceFirebaseProjectRead(d, meta) | ||
} | ||
|
||
func resourceFirebaseProjectRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
url, err := replaceVars(d, config, "{{FirebaseBasePath}}projects/{{project}}/{{name}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := sendRequest(config, "GET", project, url, nil) | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("FirebaseProject %q", d.Id())) | ||
} | ||
|
||
if err := d.Set("project", project); err != nil { | ||
return fmt.Errorf("Error reading Project: %s", err) | ||
} | ||
|
||
if err := d.Set("project_number", flattenFirebaseProjectProjectNumber(res["projectNumber"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading Project: %s", err) | ||
} | ||
if err := d.Set("display_name", flattenFirebaseProjectDisplayName(res["displayName"], d, config)); err != nil { | ||
return fmt.Errorf("Error reading Project: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceFirebaseProjectDelete(d *schema.ResourceData, meta interface{}) error { | ||
log.Printf("[WARNING] Firebase Project resources"+ | ||
" cannot be deleted from GCP. The resource %s will be removed from Terraform"+ | ||
" state, but will still be present on the server.", d.Id()) | ||
d.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
func resourceFirebaseProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
config := meta.(*Config) | ||
if err := parseImportId([]string{ | ||
"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}}/{{name}}") | ||
if err != nil { | ||
return nil, fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func flattenFirebaseProjectProjectNumber(v interface{}, d *schema.ResourceData, config *Config) interface{} { | ||
return v | ||
} | ||
|
||
func flattenFirebaseProjectDisplayName(v interface{}, d *schema.ResourceData, config *Config) interface{} { | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// 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/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccFirebaseProject_firebaseProjectBasicExample(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"org_id": getTestOrgFromEnv(t), | ||
"random_suffix": acctest.RandString(10), | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProvidersOiCS, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccFirebaseProject_firebaseProjectBasicExample(context), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccFirebaseProject_firebaseProjectBasicExample(context map[string]interface{}) string { | ||
return Nprintf(` | ||
resource "google_project" "default" { | ||
provider = google-beta | ||
project_id = "tf-test%{random_suffix}" | ||
name = "tf-test%{random_suffix}" | ||
org_id = "%{org_id}" | ||
} | ||
resource "google_firebase_project" "default" { | ||
provider = google-beta | ||
project = google_project.default.project_id | ||
} | ||
`, context) | ||
} |
Oops, something went wrong.