Skip to content
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

Promote monitoring-monitoredproject to GA #11974

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6167.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
monitoring: Promoted 'monitoredproject' to GA
```
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_firebaserules_release": resourceFirebaserulesRelease(),
"google_firebaserules_ruleset": resourceFirebaserulesRuleset(),
"google_logging_log_view": resourceLoggingLogView(),
"google_monitoring_monitored_project": resourceMonitoringMonitoredProject(),
"google_network_connectivity_hub": resourceNetworkConnectivityHub(),
"google_network_connectivity_spoke": resourceNetworkConnectivitySpoke(),
"google_org_policy_policy": resourceOrgPolicyPolicy(),
Expand Down
24 changes: 24 additions & 0 deletions google/provider_dcl_client_creation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
eventarc "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/eventarc"
firebaserules "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/firebaserules"
logging "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/logging"
monitoring "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/monitoring"
networkconnectivity "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/networkconnectivity"
orgpolicy "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/orgpolicy"
osconfig "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/osconfig"
Expand Down Expand Up @@ -362,6 +363,29 @@ func NewDCLLoggingClient(config *Config, userAgent, billingProject string, timeo
return logging.NewClient(dclConfig)
}

func NewDCLMonitoringClient(config *Config, userAgent, billingProject string, timeout time.Duration) *monitoring.Client {
configOptions := []dcl.ConfigOption{
dcl.WithHTTPClient(config.client),
dcl.WithUserAgent(userAgent),
dcl.WithLogger(dclLogger{}),
dcl.WithBasePath(config.MonitoringBasePath),
}

if timeout != 0 {
configOptions = append(configOptions, dcl.WithTimeout(timeout))
}

if config.UserProjectOverride {
configOptions = append(configOptions, dcl.WithUserProjectOverride())
if billingProject != "" {
configOptions = append(configOptions, dcl.WithBillingProject(billingProject))
}
}

dclConfig := dcl.NewConfig(configOptions...)
return monitoring.NewClient(dclConfig)
}

func NewDCLNetworkConnectivityClient(config *Config, userAgent, billingProject string, timeout time.Duration) *networkconnectivity.Client {
configOptions := []dcl.ConfigOption{
dcl.WithHTTPClient(config.client),
Expand Down
12 changes: 12 additions & 0 deletions google/provider_dcl_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ var FirebaserulesEndpointEntry = &schema.Schema{
}, ""),
}

var MonitoringEndpointEntryKey = "monitoring_custom_endpoint"
var MonitoringEndpointEntry = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_MONITORING_CUSTOM_ENDPOINT",
}, ""),
}

var NetworkConnectivityEndpointEntryKey = "network_connectivity_custom_endpoint"
var NetworkConnectivityEndpointEntry = &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -130,6 +139,7 @@ type DCLConfig struct {
DataplexBasePath string
EventarcBasePath string
FirebaserulesBasePath string
MonitoringBasePath string
NetworkConnectivityBasePath string
OrgPolicyBasePath string
RecaptchaEnterpriseBasePath string
Expand All @@ -144,6 +154,7 @@ func configureDCLProvider(provider *schema.Provider) {
provider.Schema[DataplexEndpointEntryKey] = DataplexEndpointEntry
provider.Schema[EventarcEndpointEntryKey] = EventarcEndpointEntry
provider.Schema[FirebaserulesEndpointEntryKey] = FirebaserulesEndpointEntry
provider.Schema[MonitoringEndpointEntryKey] = MonitoringEndpointEntry
provider.Schema[NetworkConnectivityEndpointEntryKey] = NetworkConnectivityEndpointEntry
provider.Schema[OrgPolicyEndpointEntryKey] = OrgPolicyEndpointEntry
provider.Schema[RecaptchaEnterpriseEndpointEntryKey] = RecaptchaEnterpriseEndpointEntry
Expand All @@ -158,6 +169,7 @@ func providerDCLConfigure(d *schema.ResourceData, config *Config) interface{} {
config.DataplexBasePath = d.Get(DataplexEndpointEntryKey).(string)
config.EventarcBasePath = d.Get(EventarcEndpointEntryKey).(string)
config.FirebaserulesBasePath = d.Get(FirebaserulesEndpointEntryKey).(string)
config.MonitoringBasePath = d.Get(MonitoringEndpointEntryKey).(string)
config.NetworkConnectivityBasePath = d.Get(NetworkConnectivityEndpointEntryKey).(string)
config.OrgPolicyBasePath = d.Get(OrgPolicyEndpointEntryKey).(string)
config.RecaptchaEnterpriseBasePath = d.Get(RecaptchaEnterpriseEndpointEntryKey).(string)
Expand Down
211 changes: 211 additions & 0 deletions google/resource_monitoring_monitored_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: DCL ***
//
// ----------------------------------------------------------------------------
//
// This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules)
// and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library).
// Changes will need to be made to the DCL or Magic Modules instead of here.
//
// We are not currently able to accept contributions to this file. If changes
// are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose
//
// ----------------------------------------------------------------------------

package google

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
monitoring "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/monitoring"
)

func resourceMonitoringMonitoredProject() *schema.Resource {
return &schema.Resource{
Create: resourceMonitoringMonitoredProjectCreate,
Read: resourceMonitoringMonitoredProjectRead,
Delete: resourceMonitoringMonitoredProjectDelete,

Importer: &schema.ResourceImporter{
State: resourceMonitoringMonitoredProjectImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},

Schema: map[string]*schema.Schema{
"metrics_scope": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Required. The resource name of the existing Metrics Scope that will monitor this project. Example: locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}",
},

"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Immutable. The resource name of the `MonitoredProject`. On input, the resource name includes the scoping project ID and monitored project ID. On output, it contains the equivalent project numbers. Example: `locations/global/metricsScopes/{SCOPING_PROJECT_ID_OR_NUMBER}/projects/{MONITORED_PROJECT_ID_OR_NUMBER}`",
},

"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. The time when this `MonitoredProject` was created.",
},
},
}
}

func resourceMonitoringMonitoredProjectCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

obj := &monitoring.MonitoredProject{
MetricsScope: dcl.String(d.Get("metrics_scope").(string)),
Name: dcl.String(d.Get("name").(string)),
}

id, err := replaceVarsForId(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
if err != nil {
return fmt.Errorf("error constructing id: %s", err)
}
d.SetId(id)
createDirective := CreateDirective
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLMonitoringClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate))
client.Config.BasePath += "v1"
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.ApplyMonitoredProject(context.Background(), obj, createDirective...)

if _, ok := err.(dcl.DiffAfterApplyError); ok {
log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err)
} else if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error creating MonitoredProject: %s", err)
}

log.Printf("[DEBUG] Finished creating MonitoredProject %q: %#v", d.Id(), res)

return resourceMonitoringMonitoredProjectRead(d, meta)
}

func resourceMonitoringMonitoredProjectRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

obj := &monitoring.MonitoredProject{
MetricsScope: dcl.String(d.Get("metrics_scope").(string)),
Name: dcl.String(d.Get("name").(string)),
}

userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLMonitoringClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead))
client.Config.BasePath += "v1"
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.GetMonitoredProject(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("MonitoringMonitoredProject %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}

if err = d.Set("metrics_scope", res.MetricsScope); err != nil {
return fmt.Errorf("error setting metrics_scope in state: %s", err)
}
if err = d.Set("name", res.Name); err != nil {
return fmt.Errorf("error setting name in state: %s", err)
}
if err = d.Set("create_time", res.CreateTime); err != nil {
return fmt.Errorf("error setting create_time in state: %s", err)
}

return nil
}

func resourceMonitoringMonitoredProjectDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

obj := &monitoring.MonitoredProject{
MetricsScope: dcl.String(d.Get("metrics_scope").(string)),
Name: dcl.String(d.Get("name").(string)),
}

log.Printf("[DEBUG] Deleting MonitoredProject %q", d.Id())
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLMonitoringClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete))
client.Config.BasePath += "v1"
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
if err := client.DeleteMonitoredProject(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting MonitoredProject: %s", err)
}

log.Printf("[DEBUG] Finished deleting MonitoredProject %q", d.Id())
return nil
}

func resourceMonitoringMonitoredProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)

if err := parseImportId([]string{
"locations/global/metricsScopes/(?P<metrics_scope>[^/]+)/projects/(?P<name>[^/]+)",
"(?P<metrics_scope>[^/]+)/(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVarsForId(d, config, "locations/global/metricsScopes/{{metrics_scope}}/projects/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}
Loading