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

Fix google_monitoring_slo import #11696

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions google/resource_monitoring_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ func resourceMonitoringSloRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("slo_id", flattenMonitoringSloSloId(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading Slo: %s", err)
}
if err := d.Set("service", flattenMonitoringSloService(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading Service: %s", err)
}

return nil
}
Expand Down Expand Up @@ -1584,6 +1587,14 @@ func flattenMonitoringSloSloId(v interface{}, d *schema.ResourceData, config *Co
return NameFromSelfLinkStateFunc(v)
}

func flattenMonitoringSloService(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
parts := strings.Split(v.(string), "/")
return parts[len(parts)-3]
}

func expandMonitoringSloDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
19 changes: 19 additions & 0 deletions google/resource_monitoring_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func getTestResourceMonitoringSloId(res string, s *terraform.State) (string, err
return "", fmt.Errorf("slo_id not set on resource %s", res)
}

func TestFlattenMonitoringSloService(t *testing.T) {
cases := map[string]struct {
Name interface{}
ExpectedService string
}{
"service is extracted from name": {
// https://cloud.google.com/monitoring/api/ref_v3/rest/v3/services.serviceLevelObjectives/get#path-parameters
Name: "projects/PROJECT_ID_OR_NUMBER/services/SERVICE_ID/serviceLevelObjectives/SLO_NAME",
ExpectedService: "SERVICE_ID",
},
}

for tn, tc := range cases {
if n := flattenMonitoringSloService(tc.Name, nil, nil); n != tc.ExpectedService {
t.Errorf("%s: expected service %q; got %q", tn, tc.ExpectedService, n)
}
}
}

func TestAccMonitoringSlo_basic(t *testing.T) {
t.Parallel()

Expand Down