Skip to content

Commit 54d2ed5

Browse files
[#13139] Add bucket_name argument to logging metric resource (#6887) (#4964)
Co-authored-by: Shuya Ma <[email protected]> Co-authored-by: Luca Prete <[email protected]> resolve hashicorp/terraform-provider-google#13139 Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 3d3467d commit 54d2ed5

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

.changelog/6887.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
logging: added `bucket_name` argument to `google_logging_metric`
3+
```

google-beta/resource_logging_metric.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ Metric identifiers are limited to 100 characters and can include only the follow
106106
characters A-Z, a-z, 0-9, and the special characters _-.,+!*',()%/. The forward-slash
107107
character (/) denotes a hierarchy of name pieces, and it cannot be the first character
108108
of the name.`,
109+
},
110+
"bucket_name": {
111+
Type: schema.TypeString,
112+
Optional: true,
113+
Description: `The resource name of the Log Bucket that owns the Log Metric. Only Log Buckets in projects
114+
are supported. The bucket has to be in the same project as the metric.`,
109115
},
110116
"bucket_options": {
111117
Type: schema.TypeList,
@@ -280,6 +286,12 @@ func resourceLoggingMetricCreate(d *schema.ResourceData, meta interface{}) error
280286
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
281287
obj["description"] = descriptionProp
282288
}
289+
bucketNameProp, err := expandLoggingMetricBucketName(d.Get("bucket_name"), d, config)
290+
if err != nil {
291+
return err
292+
} else if v, ok := d.GetOkExists("bucket_name"); !isEmptyValue(reflect.ValueOf(bucketNameProp)) && (ok || !reflect.DeepEqual(v, bucketNameProp)) {
293+
obj["bucketName"] = bucketNameProp
294+
}
283295
filterProp, err := expandLoggingMetricFilter(d.Get("filter"), d, config)
284296
if err != nil {
285297
return err
@@ -412,6 +424,9 @@ func resourceLoggingMetricRead(d *schema.ResourceData, meta interface{}) error {
412424
if err := d.Set("description", flattenLoggingMetricDescription(res["description"], d, config)); err != nil {
413425
return fmt.Errorf("Error reading Metric: %s", err)
414426
}
427+
if err := d.Set("bucket_name", flattenLoggingMetricBucketName(res["bucketName"], d, config)); err != nil {
428+
return fmt.Errorf("Error reading Metric: %s", err)
429+
}
415430
if err := d.Set("filter", flattenLoggingMetricFilter(res["filter"], d, config)); err != nil {
416431
return fmt.Errorf("Error reading Metric: %s", err)
417432
}
@@ -459,6 +474,12 @@ func resourceLoggingMetricUpdate(d *schema.ResourceData, meta interface{}) error
459474
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
460475
obj["description"] = descriptionProp
461476
}
477+
bucketNameProp, err := expandLoggingMetricBucketName(d.Get("bucket_name"), d, config)
478+
if err != nil {
479+
return err
480+
} else if v, ok := d.GetOkExists("bucket_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, bucketNameProp)) {
481+
obj["bucketName"] = bucketNameProp
482+
}
462483
filterProp, err := expandLoggingMetricFilter(d.Get("filter"), d, config)
463484
if err != nil {
464485
return err
@@ -584,6 +605,10 @@ func flattenLoggingMetricDescription(v interface{}, d *schema.ResourceData, conf
584605
return v
585606
}
586607

608+
func flattenLoggingMetricBucketName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
609+
return v
610+
}
611+
587612
func flattenLoggingMetricFilter(v interface{}, d *schema.ResourceData, config *Config) interface{} {
588613
return v
589614
}
@@ -795,6 +820,10 @@ func expandLoggingMetricDescription(v interface{}, d TerraformResourceData, conf
795820
return v, nil
796821
}
797822

823+
func expandLoggingMetricBucketName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
824+
return v, nil
825+
}
826+
798827
func expandLoggingMetricFilter(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
799828
return v, nil
800829
}

google-beta/resource_logging_metric_generated_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,53 @@ resource "google_logging_metric" "logging_metric" {
166166
`, context)
167167
}
168168

169+
func TestAccLoggingMetric_loggingMetricLoggingBucketExample(t *testing.T) {
170+
t.Parallel()
171+
172+
context := map[string]interface{}{
173+
"project": getTestProjectFromEnv(),
174+
"random_suffix": randString(t, 10),
175+
}
176+
177+
vcrTest(t, resource.TestCase{
178+
PreCheck: func() { testAccPreCheck(t) },
179+
Providers: testAccProviders,
180+
CheckDestroy: testAccCheckLoggingMetricDestroyProducer(t),
181+
Steps: []resource.TestStep{
182+
{
183+
Config: testAccLoggingMetric_loggingMetricLoggingBucketExample(context),
184+
},
185+
{
186+
ResourceName: "google_logging_metric.logging_metric",
187+
ImportState: true,
188+
ImportStateVerify: true,
189+
},
190+
},
191+
})
192+
}
193+
194+
func testAccLoggingMetric_loggingMetricLoggingBucketExample(context map[string]interface{}) string {
195+
return Nprintf(`
196+
resource "google_logging_project_bucket_config" "logging_metric" {
197+
location = "global"
198+
project = "%{project}"
199+
bucket_id = "_Default"
200+
}
201+
202+
resource "google_logging_metric" "logging_metric" {
203+
name = "tf-test-my-(custom)/metric%{random_suffix}"
204+
filter = "resource.type=gae_app AND severity>=ERROR"
205+
bucket_name = google_logging_project_bucket_config.logging_metric.id
206+
207+
metric_descriptor {
208+
metric_kind = "DELTA"
209+
value_type = "INT64"
210+
unit = "1"
211+
}
212+
}
213+
`, context)
214+
}
215+
169216
func testAccCheckLoggingMetricDestroyProducer(t *testing.T) func(s *terraform.State) error {
170217
return func(s *terraform.State) error {
171218
for name, rs := range s.RootModule().Resources {

google-beta/resource_logging_metric_test.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,46 @@ func TestAccLoggingMetric_explicitBucket(t *testing.T) {
6262
})
6363
}
6464

65+
func TestAccLoggingMetric_loggingBucket(t *testing.T) {
66+
t.Parallel()
67+
68+
filter := "resource.type=gae_app AND severity>=ERROR"
69+
project_id := getTestProjectFromEnv()
70+
suffix := randString(t, 10)
71+
72+
vcrTest(t, resource.TestCase{
73+
PreCheck: func() { testAccPreCheck(t) },
74+
Providers: testAccProviders,
75+
CheckDestroy: testAccCheckLoggingMetricDestroyProducer(t),
76+
Steps: []resource.TestStep{
77+
{
78+
Config: testAccLoggingMetric_loggingBucketBase(suffix, filter),
79+
},
80+
{
81+
ResourceName: "google_logging_metric.logging_metric",
82+
ImportState: true,
83+
ImportStateVerify: true,
84+
},
85+
{
86+
Config: testAccLoggingMetric_loggingBucket(suffix, filter, project_id),
87+
},
88+
{
89+
ResourceName: "google_logging_metric.logging_metric",
90+
ImportState: true,
91+
ImportStateVerify: true,
92+
},
93+
{
94+
Config: testAccLoggingMetric_loggingBucketBase(suffix, filter),
95+
},
96+
{
97+
ResourceName: "google_logging_metric.logging_metric",
98+
ImportState: true,
99+
ImportStateVerify: true,
100+
},
101+
},
102+
})
103+
}
104+
65105
func TestAccLoggingMetric_descriptionUpdated(t *testing.T) {
66106
t.Parallel()
67107

@@ -128,11 +168,48 @@ resource "google_logging_metric" "logging_metric" {
128168
`, suffix, filter)
129169
}
130170

171+
func testAccLoggingMetric_loggingBucketBase(suffix string, filter string) string {
172+
return fmt.Sprintf(`
173+
resource "google_logging_metric" "logging_metric" {
174+
name = "my-custom-metric-%s"
175+
filter = "%s"
176+
177+
metric_descriptor {
178+
metric_kind = "DELTA"
179+
unit = "1"
180+
value_type = "INT64"
181+
}
182+
}
183+
`, suffix, filter)
184+
}
185+
186+
func testAccLoggingMetric_loggingBucket(suffix string, filter string, project_id string) string {
187+
return fmt.Sprintf(`
188+
resource "google_logging_project_bucket_config" "logging_bucket" {
189+
location = "global"
190+
project = "%s"
191+
bucket_id = "_Default"
192+
}
193+
194+
resource "google_logging_metric" "logging_metric" {
195+
name = "my-custom-metric-%s"
196+
bucket_name = google_logging_project_bucket_config.logging_bucket.id
197+
filter = "%s"
198+
199+
metric_descriptor {
200+
metric_kind = "DELTA"
201+
unit = "1"
202+
value_type = "INT64"
203+
}
204+
}
205+
`, project_id, suffix, filter)
206+
}
207+
131208
func testAccLoggingMetric_descriptionUpdated(suffix, description string) string {
132209
return fmt.Sprintf(`
133210
resource "google_logging_metric" "logging_metric" {
134211
name = "my-custom-metric-%s"
135-
description = "Counter for VM instances that have hostError's"
212+
description = "Counter for VM instances that have hostError's"
136213
filter = "resource.type=gce_instance AND protoPayload.methodName=compute.instances.hostError"
137214
metric_descriptor {
138215
metric_kind = "DELTA"

website/docs/r/logging_metric.html.markdown

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ resource "google_logging_metric" "logging_metric" {
118118
}
119119
}
120120
```
121+
## Example Usage - Logging Metric Logging Bucket
122+
123+
124+
```hcl
125+
resource "google_logging_project_bucket_config" "logging_metric" {
126+
location = "global"
127+
project = "my-project-name"
128+
bucket_id = "_Default"
129+
}
130+
131+
resource "google_logging_metric" "logging_metric" {
132+
name = "my-(custom)/metric"
133+
filter = "resource.type=gae_app AND severity>=ERROR"
134+
bucket_name = google_logging_project_bucket_config.logging_metric.id
135+
136+
metric_descriptor {
137+
metric_kind = "DELTA"
138+
value_type = "INT64"
139+
unit = "1"
140+
}
141+
}
142+
```
121143

122144
## Argument Reference
123145

@@ -204,6 +226,11 @@ The following arguments are supported:
204226
A description of this metric, which is used in documentation. The maximum length of the
205227
description is 8000 characters.
206228

229+
* `bucket_name` -
230+
(Optional)
231+
The resource name of the Log Bucket that owns the Log Metric. Only Log Buckets in projects
232+
are supported. The bucket has to be in the same project as the metric.
233+
207234
* `label_extractors` -
208235
(Optional)
209236
A map from a label key string to an extractor expression which is used to extract data from a log

0 commit comments

Comments
 (0)