From 77c757d1e087048a02a8a26dd8a7a0e3b4ab38bc Mon Sep 17 00:00:00 2001 From: Chris Stephens Date: Tue, 13 Nov 2018 21:33:51 +0000 Subject: [PATCH] New Resource: Stack Driver Group --- google/provider_monitoring_gen.go | 1 + google/resource_monitoring_group.go | 295 ++++++++++++++++++ ...esource_monitoring_group_generated_test.go | 113 +++++++ google/resource_monitoring_group_test.go | 47 +++ .../docs/r/container_cluster.html.markdown | 5 - website/docs/r/monitoring_group.html.markdown | 123 ++++++++ 6 files changed, 579 insertions(+), 5 deletions(-) create mode 100644 google/resource_monitoring_group.go create mode 100644 google/resource_monitoring_group_generated_test.go create mode 100644 google/resource_monitoring_group_test.go create mode 100644 website/docs/r/monitoring_group.html.markdown diff --git a/google/provider_monitoring_gen.go b/google/provider_monitoring_gen.go index 484e473a..feba9e1f 100644 --- a/google/provider_monitoring_gen.go +++ b/google/provider_monitoring_gen.go @@ -18,4 +18,5 @@ import "github.com/hashicorp/terraform/helper/schema" var GeneratedMonitoringResourcesMap = map[string]*schema.Resource{ "google_monitoring_alert_policy": resourceMonitoringAlertPolicy(), + "google_monitoring_group": resourceMonitoringGroup(), } diff --git a/google/resource_monitoring_group.go b/google/resource_monitoring_group.go new file mode 100644 index 00000000..8712b1cd --- /dev/null +++ b/google/resource_monitoring_group.go @@ -0,0 +1,295 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "reflect" + + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceMonitoringGroup() *schema.Resource { + return &schema.Resource{ + Create: resourceMonitoringGroupCreate, + Read: resourceMonitoringGroupRead, + Update: resourceMonitoringGroupUpdate, + Delete: resourceMonitoringGroupDelete, + + Importer: &schema.ResourceImporter{ + State: resourceMonitoringGroupImport, + }, + + Schema: map[string]*schema.Schema{ + "display_name": { + Type: schema.TypeString, + Required: true, + }, + "filter": { + Type: schema.TypeString, + Required: true, + }, + "is_cluster": { + Type: schema.TypeBool, + Optional: true, + }, + "parent_name": { + Type: schema.TypeString, + Optional: true, + DiffSuppressFunc: compareSelfLinkRelativePaths, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + } +} + +func resourceMonitoringGroupCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + obj := make(map[string]interface{}) + parentNameProp, err := expandMonitoringGroupParentName(d.Get("parent_name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("parent_name"); !isEmptyValue(reflect.ValueOf(parentNameProp)) && (ok || !reflect.DeepEqual(v, parentNameProp)) { + obj["parentName"] = parentNameProp + } + isClusterProp, err := expandMonitoringGroupIsCluster(d.Get("is_cluster"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("is_cluster"); !isEmptyValue(reflect.ValueOf(isClusterProp)) && (ok || !reflect.DeepEqual(v, isClusterProp)) { + obj["isCluster"] = isClusterProp + } + displayNameProp, err := expandMonitoringGroupDisplayName(d.Get("display_name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) { + obj["displayName"] = displayNameProp + } + filterProp, err := expandMonitoringGroupFilter(d.Get("filter"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("filter"); !isEmptyValue(reflect.ValueOf(filterProp)) && (ok || !reflect.DeepEqual(v, filterProp)) { + obj["filter"] = filterProp + } + + lockName, err := replaceVars(d, config, "stackdriver/groups/{{project}}") + if err != nil { + return err + } + mutexKV.Lock(lockName) + defer mutexKV.Unlock(lockName) + + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/projects/{{project}}/groups") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Group: %#v", obj) + res, err := sendRequest(config, "POST", url, obj) + if err != nil { + return fmt.Errorf("Error creating Group: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + log.Printf("[DEBUG] Finished creating Group %q: %#v", d.Id(), res) + + // `name` is autogenerated from the api so needs to be set post-create + name, ok := res["name"] + if !ok { + return fmt.Errorf("Create response didn't contain critical fields. Create may not have succeeded.") + } + d.Set("name", name.(string)) + d.SetId(name.(string)) + + return resourceMonitoringGroupRead(d, meta) +} + +func resourceMonitoringGroupRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/{{name}}") + if err != nil { + return err + } + + res, err := sendRequest(config, "GET", url, nil) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("MonitoringGroup %q", d.Id())) + } + + if err := d.Set("parent_name", flattenMonitoringGroupParentName(res["parentName"])); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("name", flattenMonitoringGroupName(res["name"])); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("is_cluster", flattenMonitoringGroupIsCluster(res["isCluster"])); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("display_name", flattenMonitoringGroupDisplayName(res["displayName"])); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + if err := d.Set("filter", flattenMonitoringGroupFilter(res["filter"])); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + project, err := getProject(d, config) + if err != nil { + return err + } + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading Group: %s", err) + } + + return nil +} + +func resourceMonitoringGroupUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + obj := make(map[string]interface{}) + parentNameProp, err := expandMonitoringGroupParentName(d.Get("parent_name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("parent_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, parentNameProp)) { + obj["parentName"] = parentNameProp + } + isClusterProp, err := expandMonitoringGroupIsCluster(d.Get("is_cluster"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("is_cluster"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, isClusterProp)) { + obj["isCluster"] = isClusterProp + } + displayNameProp, err := expandMonitoringGroupDisplayName(d.Get("display_name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) { + obj["displayName"] = displayNameProp + } + filterProp, err := expandMonitoringGroupFilter(d.Get("filter"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("filter"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, filterProp)) { + obj["filter"] = filterProp + } + + lockName, err := replaceVars(d, config, "stackdriver/groups/{{project}}") + if err != nil { + return err + } + mutexKV.Lock(lockName) + defer mutexKV.Unlock(lockName) + + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/{{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating Group %q: %#v", d.Id(), obj) + _, err = sendRequest(config, "PUT", url, obj) + + if err != nil { + return fmt.Errorf("Error updating Group %q: %s", d.Id(), err) + } + + return resourceMonitoringGroupRead(d, meta) +} + +func resourceMonitoringGroupDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + lockName, err := replaceVars(d, config, "stackdriver/groups/{{project}}") + if err != nil { + return err + } + mutexKV.Lock(lockName) + defer mutexKV.Unlock(lockName) + + url, err := replaceVars(d, config, "https://monitoring.googleapis.com/v3/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting Group %q", d.Id()) + res, err := sendRequest(config, "DELETE", url, obj) + if err != nil { + return handleNotFoundError(err, d, "Group") + } + + log.Printf("[DEBUG] Finished deleting Group %q: %#v", d.Id(), res) + return nil +} + +func resourceMonitoringGroupImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + + config := meta.(*Config) + + // current import_formats can't import id's with forward slashes in them. + parseImportId([]string{"(?P.+)"}, d, config) + + return []*schema.ResourceData{d}, nil +} + +func flattenMonitoringGroupParentName(v interface{}) interface{} { + return v +} + +func flattenMonitoringGroupName(v interface{}) interface{} { + return v +} + +func flattenMonitoringGroupIsCluster(v interface{}) interface{} { + return v +} + +func flattenMonitoringGroupDisplayName(v interface{}) interface{} { + return v +} + +func flattenMonitoringGroupFilter(v interface{}) interface{} { + return v +} + +func expandMonitoringGroupParentName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandMonitoringGroupIsCluster(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandMonitoringGroupDisplayName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandMonitoringGroupFilter(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google/resource_monitoring_group_generated_test.go b/google/resource_monitoring_group_generated_test.go new file mode 100644 index 00000000..3a217987 --- /dev/null +++ b/google/resource_monitoring_group_generated_test.go @@ -0,0 +1,113 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccMonitoringGroup_monitoringGroupBasicExample(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckMonitoringGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccMonitoringGroup_monitoringGroupBasicExample(acctest.RandString(10)), + }, + { + ResourceName: "google_monitoring_group.basic", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccMonitoringGroup_monitoringGroupBasicExample(val string) string { + return fmt.Sprintf(` +resource "google_monitoring_group" "basic" { + display_name = "New Test Group-%s" + + filter = "resource.metadata.region=\"europe-west2\"" +} +`, val, + ) +} + +func TestAccMonitoringGroup_monitoringGroupSubgroupExample(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckMonitoringGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccMonitoringGroup_monitoringGroupSubgroupExample(acctest.RandString(10)), + }, + { + ResourceName: "google_monitoring_group.subgroup", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccMonitoringGroup_monitoringGroupSubgroupExample(val string) string { + return fmt.Sprintf(` +resource "google_monitoring_group" "parent" { + display_name = "New Test SubGroup-%s" + filter = "resource.metadata.region=\"europe-west2\"" +} + +resource "google_monitoring_group" "subgroup" { + display_name = "New Test SubGroup-%s" + filter = "resource.metadata.region=\"europe-west2\"" + parent_name = "${google_monitoring_group.parent.name}" +} +`, val, val, + ) +} + +func testAccCheckMonitoringGroupDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_monitoring_group" { + continue + } + + config := testAccProvider.Meta().(*Config) + + url, err := replaceVarsForTest(rs, "https://monitoring.googleapis.com/v3/{{name}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", url, nil) + if err == nil { + return fmt.Errorf("MonitoringGroup still exists at %s", url) + } + } + + return nil +} diff --git a/google/resource_monitoring_group_test.go b/google/resource_monitoring_group_test.go new file mode 100644 index 00000000..e5b70b0d --- /dev/null +++ b/google/resource_monitoring_group_test.go @@ -0,0 +1,47 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccMonitoringGroup_update(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckMonitoringGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccMonitoringGroup_update("europe-west1"), + }, + { + ResourceName: "google_monitoring_group.update", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccMonitoringGroup_update("europe-west2"), + }, + { + ResourceName: "google_monitoring_group.update", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccMonitoringGroup_update(zone string) string { + return fmt.Sprintf(` +resource "google_monitoring_group" "update" { + display_name = "Integration Test Group" + + filter = "resource.metadata.region=\"%s\"" +} +`, zone, + ) +} diff --git a/website/docs/r/container_cluster.html.markdown b/website/docs/r/container_cluster.html.markdown index f105c2c2..cf47a9db 100644 --- a/website/docs/r/container_cluster.html.markdown +++ b/website/docs/r/container_cluster.html.markdown @@ -357,11 +357,6 @@ The `node_config` block supports: * `service_account` - (Optional) The service account to be used by the Node VMs. If not specified, the "default" service account is used. - In order to use the configured `oauth_scopes` for logging and monitoring, the service account being used needs the - [roles/logging.logWriter](https://cloud.google.com/iam/docs/understanding-roles#stackdriver_logging_roles) and - [roles/monitoring.metricWriter](https://cloud.google.com/iam/docs/understanding-roles#stackdriver_monitoring_roles) roles. - - -> Projects that enable the [Cloud Compute Engine API](https://cloud.google.com/compute/) with Terraform may need these roles added manually to the service account. Projects that enable the API in the Cloud Console should have them added automatically. * `tags` - (Optional) The list of instance tags applied to all nodes. Tags are used to identify valid sources or targets for network firewalls. diff --git a/website/docs/r/monitoring_group.html.markdown b/website/docs/r/monitoring_group.html.markdown new file mode 100644 index 00000000..25a7af32 --- /dev/null +++ b/website/docs/r/monitoring_group.html.markdown @@ -0,0 +1,123 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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. +# +# ---------------------------------------------------------------------------- +layout: "google" +page_title: "Google: google_monitoring_group" +sidebar_current: "docs-google-monitoring-group" +description: |- + The description of a dynamic collection of monitored resources. +--- + +# google\_monitoring\_group + +The description of a dynamic collection of monitored resources. Each group +has a filter that is matched against monitored resources and their +associated metadata. If a group's filter matches an available monitored +resource, then that resource is a member of that group. + + +To get more information about Group, see: + +* [API documentation](https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.groups) +* How-to Guides + * [Official Documentation](https://cloud.google.com/monitoring/groups/) + + +## Example Usage - Monitoring Group Basic + + +```hcl +resource "google_monitoring_group" "basic" { + display_name = "New Test Group" + + filter = "resource.metadata.region=\"europe-west2\"" +} +``` + +## Example Usage - Monitoring Group Subgroup + + +```hcl +resource "google_monitoring_group" "parent" { + display_name = "New Test SubGroup" + filter = "resource.metadata.region=\"europe-west2\"" +} + +resource "google_monitoring_group" "subgroup" { + display_name = "New Test SubGroup" + filter = "resource.metadata.region=\"europe-west2\"" + parent_name = "${google_monitoring_group.parent.name}" +} +``` + +## Argument Reference + +The following arguments are supported: + + +* `display_name` - + (Required) + A user-assigned name for this group, used only for display + purposes. + +* `filter` - + (Required) + The filter used to determine which monitored resources + belong to this group. + + +- - - + + +* `parent_name` - + (Optional) + The name of the group's parent, if it has one. The format is + "projects/{project_id_or_number}/groups/{group_id}". For + groups with no parent, parentName is the empty string, "". + +* `is_cluster` - + (Optional) + If true, the members of this group are considered to be a + cluster. The system can perform additional analysis on + groups that are clusters. +* `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: + + +* `name` - + A unique identifier for this group. The format is + "projects/{project_id_or_number}/groups/{group_id}". + + + +## Import + +Group can be imported using any of these accepted formats: + +``` +$ terraform import google_monitoring_group.default {{name}} +```