-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new resource for compute default network tier (#3907)
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
de903bf
commit 928a6c6
Showing
6 changed files
with
257 additions
and
7 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
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,93 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"google.golang.org/api/compute/v1" | ||
) | ||
|
||
func resourceComputeProjectDefaultNetworkTier() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceComputeProjectDefaultNetworkTierCreateOrUpdate, | ||
Read: resourceComputeProjectDefaultNetworkTierRead, | ||
Update: resourceComputeProjectDefaultNetworkTierCreateOrUpdate, | ||
Delete: resourceComputeProjectDefaultNetworkTierDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
SchemaVersion: 0, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"network_tier": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"PREMIUM", "STANDARD"}, false), | ||
}, | ||
|
||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceComputeProjectDefaultNetworkTierCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
projectID, err := getProject(d, config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
request := &compute.ProjectsSetDefaultNetworkTierRequest{ | ||
NetworkTier: d.Get("network_tier").(string), | ||
} | ||
op, err := config.clientCompute.Projects.SetDefaultNetworkTier(projectID, request).Do() | ||
if err != nil { | ||
return fmt.Errorf("SetDefaultNetworkTier failed: %s", err) | ||
} | ||
|
||
log.Printf("[DEBUG] SetDefaultNetworkTier: %d (%s)", op.Id, op.SelfLink) | ||
err = computeOperationWait(config.clientCompute, op, projectID, "SetDefaultNetworkTier") | ||
if err != nil { | ||
return fmt.Errorf("SetDefaultNetworkTier failed: %s", err) | ||
} | ||
|
||
d.SetId(projectID) | ||
|
||
return resourceComputeProjectDefaultNetworkTierRead(d, meta) | ||
} | ||
|
||
func resourceComputeProjectDefaultNetworkTierRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
projectId := d.Id() | ||
|
||
project, err := config.clientCompute.Projects.Get(projectId).Do() | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("Project data for project %q", projectId)) | ||
} | ||
|
||
err = d.Set("network_tier", project.DefaultNetworkTier) | ||
if err != nil { | ||
return fmt.Errorf("Error setting default network tier: %s", err) | ||
} | ||
|
||
d.Set("project", projectId) | ||
|
||
return nil | ||
} | ||
|
||
func resourceComputeProjectDefaultNetworkTierDelete(d *schema.ResourceData, meta interface{}) error { | ||
|
||
log.Printf("[WARNING] Default Network Tier will be only removed from Terraform state, but will be left intact on GCP.") | ||
|
||
return schema.RemoveFromState(d, meta) | ||
} |
106 changes: 106 additions & 0 deletions
106
google/resource_compute_project_default_network_tier_test.go
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,106 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccComputeProjectDefaultNetworkTier_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
org := getTestOrgFromEnv(t) | ||
billingId := getTestBillingAccountFromEnv(t) | ||
projectID := "terraform-test-" + acctest.RandString(10) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccComputeProject_defaultNetworkTier_premium(projectID, pname, org, billingId), | ||
}, | ||
{ | ||
ResourceName: "google_compute_project_default_network_tier.fizzbuzz", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccComputeProjectDefaultNetworkTier_modify(t *testing.T) { | ||
t.Parallel() | ||
|
||
org := getTestOrgFromEnv(t) | ||
billingId := getTestBillingAccountFromEnv(t) | ||
projectID := "terraform-test-" + acctest.RandString(10) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccComputeProject_defaultNetworkTier_premium(projectID, pname, org, billingId), | ||
}, | ||
{ | ||
ResourceName: "google_compute_project_default_network_tier.fizzbuzz", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
|
||
{ | ||
Config: testAccComputeProject_defaultNetworkTier_standard(projectID, pname, org, billingId), | ||
}, | ||
{ | ||
ResourceName: "google_compute_project_default_network_tier.fizzbuzz", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccComputeProject_defaultNetworkTier_premium(projectID, name, org, billing string) string { | ||
return fmt.Sprintf(` | ||
resource "google_project" "project" { | ||
project_id = "%s" | ||
name = "%s" | ||
org_id = "%s" | ||
billing_account = "%s" | ||
} | ||
resource "google_project_service" "compute" { | ||
project = "${google_project.project.project_id}" | ||
service = "compute.googleapis.com" | ||
} | ||
resource "google_compute_project_default_network_tier" "fizzbuzz" { | ||
project = "${google_project.project.project_id}" | ||
network_tier = "PREMIUM" | ||
depends_on = ["google_project_service.compute"] | ||
}`, projectID, name, org, billing) | ||
} | ||
|
||
func testAccComputeProject_defaultNetworkTier_standard(projectID, name, org, billing string) string { | ||
return fmt.Sprintf(` | ||
resource "google_project" "project" { | ||
project_id = "%s" | ||
name = "%s" | ||
org_id = "%s" | ||
billing_account = "%s" | ||
} | ||
resource "google_project_service" "compute" { | ||
project = "${google_project.project.project_id}" | ||
service = "compute.googleapis.com" | ||
} | ||
resource "google_compute_project_default_network_tier" "fizzbuzz" { | ||
project = "${google_project.project.project_id}" | ||
network_tier = "STANDARD" | ||
depends_on = ["google_project_service.compute"] | ||
}`, projectID, name, org, billing) | ||
} |
46 changes: 46 additions & 0 deletions
46
website/docs/r/compute_project_default_network_tier.html.markdown
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,46 @@ | ||
--- | ||
layout: "google" | ||
page_title: "Google: google_compute_project_default_network_tier" | ||
sidebar_current: "docs-google-compute-project-default-network-tier" | ||
description: |- | ||
Configures the default network tier for a project. | ||
--- | ||
|
||
# google\_compute\_project\_default\_network\_tier | ||
|
||
Configures the Google Compute Engine | ||
[Default Network Tier](https://cloud.google.com/network-tiers/docs/using-network-service-tiers#setting_the_tier_for_all_resources_in_a_project) | ||
for a project. | ||
|
||
For more information, see, | ||
[the Project API documentation](https://cloud.google.com/compute/docs/reference/rest/v1/projects/setDefaultNetworkTier). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "google_compute_project_default_network_tier" "default" { | ||
network_tier = "PREMIUM" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `network_tier` - (Required) The default network tier to be configured for the project. | ||
This field can take the following values: `PREMIUM` or `STANDARD`. | ||
|
||
- - - | ||
|
||
* `project` - (Optional) The ID of the project in which the resource belongs. If it | ||
is not provided, the provider project is used. | ||
|
||
## Attributes Reference | ||
|
||
Only the arguments listed above are exposed as attributes. | ||
|
||
## Import | ||
|
||
This resource can be imported using the project ID: | ||
|
||
`terraform import google_compute_project_default_network_tier.default project-id` |
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