-
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 new google_project_service
resource for fine-grained service control.
#668
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,87 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceGoogleProjectService() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceGoogleProjectServiceCreate, | ||
Read: resourceGoogleProjectServiceRead, | ||
Delete: resourceGoogleProjectServiceDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"service": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"project": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceGoogleProjectServiceCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
srv := d.Get("service").(string) | ||
|
||
if err = enableService(srv, project, config); err != nil { | ||
return fmt.Errorf("Error enabling service: %s", err) | ||
} | ||
|
||
d.SetId(srv) | ||
return resourceGoogleProjectServiceRead(d, meta) | ||
} | ||
|
||
func resourceGoogleProjectServiceRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
services, err := getApiServices(project, config, map[string]struct{}{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, s := range services { | ||
if s == d.Id() { | ||
d.Set("service", s) | ||
return nil | ||
} | ||
} | ||
|
||
// The service is not enabled server-side, so remove it from state | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func resourceGoogleProjectServiceDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
project, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = disableService(d.Id(), project, config); err != nil { | ||
return fmt.Errorf("Error disabling service: %s", err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
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,85 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
// Test that services can be enabled and disabled on a project | ||
func TestAccGoogleProjectService_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
pid := "terraform-" + acctest.RandString(10) | ||
services := []string{"iam.googleapis.com", "cloudresourcemanager.googleapis.com"} | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccGoogleProjectService_basic(services, pid, pname, org), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckProjectService(services, pid, true), | ||
), | ||
}, | ||
// Use a separate TestStep rather than a CheckDestroy because we need the project to still exist. | ||
resource.TestStep{ | ||
Config: testAccGoogleProject_create(pid, pname, org), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckProjectService(services, pid, false), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckProjectService(services []string, pid string, expectEnabled bool) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
apiServices, err := getApiServices(pid, config, map[string]struct{}{}) | ||
if err != nil { | ||
return fmt.Errorf("Error listing services for project %q: %v", pid, err) | ||
} | ||
|
||
for _, expected := range services { | ||
exists := false | ||
for _, actual := range apiServices { | ||
if expected == actual { | ||
exists = true | ||
} | ||
} | ||
if expectEnabled && !exists { | ||
return fmt.Errorf("Expected service %s is not enabled server-side (found %v)", expected, apiServices) | ||
} | ||
if !expectEnabled && exists { | ||
return fmt.Errorf("Expected disabled service %s is enabled server-side", expected) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccGoogleProjectService_basic(services []string, pid, name, org string) string { | ||
return fmt.Sprintf(` | ||
resource "google_project" "acceptance" { | ||
project_id = "%s" | ||
name = "%s" | ||
org_id = "%s" | ||
} | ||
|
||
resource "google_project_service" "test" { | ||
project = "${google_project.acceptance.project_id}" | ||
service = "%s" | ||
} | ||
|
||
resource "google_project_service" "test2" { | ||
project = "${google_project.acceptance.project_id}" | ||
service = "%s" | ||
} | ||
`, pid, name, org, services[0], services[1]) | ||
} |
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
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,34 @@ | ||
--- | ||
layout: "google" | ||
page_title: "Google: google_project_service" | ||
sidebar_current: "docs-google-project-service" | ||
description: |- | ||
Allows management of a single API service for a Google Cloud Platform project. | ||
--- | ||
|
||
# google\_project\_service | ||
|
||
Allows management of a single API service for an existing Google Cloud Platform project. | ||
|
||
For a list of services available, visit the | ||
[API library page](https://console.cloud.google.com/apis/library) or run `gcloud service-management list`. | ||
|
||
~> **Note:** This resource _must not_ be used in conjunction with | ||
`google_project_services` or they will fight over which services should be enabled. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "google_project_service" "project" { | ||
project = "your-project-id" | ||
service = "iam.googleapis.com" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `service` - (Required) The service to enable. | ||
|
||
* `project` - (Optional) The project ID. If not provided, the provider project is used. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we include the project in this, so multiple projects can enable the same service within the same Terraform state?
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.
Sure! Done.