Skip to content

Commit 996b85f

Browse files
modular-magicianupodroidc2thorn
authored
Add support for processing_units to google_spanner_instance (#4993) (#3479)
Co-authored-by: upodroid <[email protected]> Co-authored-by: Cameron Thornton <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: upodroid <[email protected]> Co-authored-by: Cameron Thornton <[email protected]>
1 parent 77e7e40 commit 996b85f

File tree

6 files changed

+157
-6
lines changed

6 files changed

+157
-6
lines changed

.changelog/4993.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:note
2+
spanner: `num_nodes` will become a required field in the next major release and a value has to be set on `google_spanner_instance`. It will also conflict with the new field `processing_units`.
3+
```
4+
```release-note:enhancement
5+
spanner: added `processing_units` to `google_spanner_instance`.
6+
```

google-beta/resource_gke_hub_feature_membership_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8-
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
8+
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
99
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

google-beta/resource_spanner_instance.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,18 @@ Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.`,
119119
Elem: &schema.Schema{Type: schema.TypeString},
120120
},
121121
"num_nodes": {
122-
Type: schema.TypeInt,
123-
Optional: true,
124-
Description: `The number of nodes allocated to this instance.`,
125-
Default: 1,
122+
Type: schema.TypeInt,
123+
Computed: true,
124+
Optional: true,
125+
Description: `The number of nodes allocated to this instance. At most one of either node_count or processing_units
126+
can be present in terraform.`,
127+
},
128+
"processing_units": {
129+
Type: schema.TypeInt,
130+
Computed: true,
131+
Optional: true,
132+
Description: `The number of processing units allocated to this instance. At most one of processing_units
133+
or node_count can be present in terraform.`,
126134
},
127135
"state": {
128136
Type: schema.TypeString,
@@ -177,6 +185,12 @@ func resourceSpannerInstanceCreate(d *schema.ResourceData, meta interface{}) err
177185
} else if v, ok := d.GetOkExists("num_nodes"); !isEmptyValue(reflect.ValueOf(nodeCountProp)) && (ok || !reflect.DeepEqual(v, nodeCountProp)) {
178186
obj["nodeCount"] = nodeCountProp
179187
}
188+
processingUnitsProp, err := expandSpannerInstanceProcessingUnits(d.Get("processing_units"), d, config)
189+
if err != nil {
190+
return err
191+
} else if v, ok := d.GetOkExists("processing_units"); !isEmptyValue(reflect.ValueOf(processingUnitsProp)) && (ok || !reflect.DeepEqual(v, processingUnitsProp)) {
192+
obj["processingUnits"] = processingUnitsProp
193+
}
180194
labelsProp, err := expandSpannerInstanceLabels(d.Get("labels"), d, config)
181195
if err != nil {
182196
return err
@@ -325,6 +339,9 @@ func resourceSpannerInstanceRead(d *schema.ResourceData, meta interface{}) error
325339
if err := d.Set("num_nodes", flattenSpannerInstanceNumNodes(res["nodeCount"], d, config)); err != nil {
326340
return fmt.Errorf("Error reading Instance: %s", err)
327341
}
342+
if err := d.Set("processing_units", flattenSpannerInstanceProcessingUnits(res["processingUnits"], d, config)); err != nil {
343+
return fmt.Errorf("Error reading Instance: %s", err)
344+
}
328345
if err := d.Set("labels", flattenSpannerInstanceLabels(res["labels"], d, config)); err != nil {
329346
return fmt.Errorf("Error reading Instance: %s", err)
330347
}
@@ -363,6 +380,12 @@ func resourceSpannerInstanceUpdate(d *schema.ResourceData, meta interface{}) err
363380
} else if v, ok := d.GetOkExists("num_nodes"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, nodeCountProp)) {
364381
obj["nodeCount"] = nodeCountProp
365382
}
383+
processingUnitsProp, err := expandSpannerInstanceProcessingUnits(d.Get("processing_units"), d, config)
384+
if err != nil {
385+
return err
386+
} else if v, ok := d.GetOkExists("processing_units"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, processingUnitsProp)) {
387+
obj["processingUnits"] = processingUnitsProp
388+
}
366389
labelsProp, err := expandSpannerInstanceLabels(d.Get("labels"), d, config)
367390
if err != nil {
368391
return err
@@ -521,6 +544,23 @@ func flattenSpannerInstanceNumNodes(v interface{}, d *schema.ResourceData, confi
521544
return v // let terraform core handle it otherwise
522545
}
523546

547+
func flattenSpannerInstanceProcessingUnits(v interface{}, d *schema.ResourceData, config *Config) interface{} {
548+
// Handles the string fixed64 format
549+
if strVal, ok := v.(string); ok {
550+
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
551+
return intVal
552+
}
553+
}
554+
555+
// number values are represented as float64
556+
if floatVal, ok := v.(float64); ok {
557+
intVal := int(floatVal)
558+
return intVal
559+
}
560+
561+
return v // let terraform core handle it otherwise
562+
}
563+
524564
func flattenSpannerInstanceLabels(v interface{}, d *schema.ResourceData, config *Config) interface{} {
525565
return v
526566
}
@@ -555,6 +595,10 @@ func expandSpannerInstanceNumNodes(v interface{}, d TerraformResourceData, confi
555595
return v, nil
556596
}
557597

598+
func expandSpannerInstanceProcessingUnits(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
599+
return v, nil
600+
}
601+
558602
func expandSpannerInstanceLabels(v interface{}, d TerraformResourceData, config *Config) (map[string]string, error) {
559603
if v == nil {
560604
return map[string]string{}, nil
@@ -567,6 +611,10 @@ func expandSpannerInstanceLabels(v interface{}, d TerraformResourceData, config
567611
}
568612

569613
func resourceSpannerInstanceEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
614+
// Temp Logic to accomodate processing_units and num_nodes
615+
if obj["processingUnits"] == nil && obj["nodeCount"] == nil {
616+
obj["nodeCount"] = 1
617+
}
570618
newObj := make(map[string]interface{})
571619
newObj["instance"] = obj
572620
if obj["name"] == nil {

google-beta/resource_spanner_instance_generated_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,45 @@ resource "google_spanner_instance" "example" {
6262
`, context)
6363
}
6464

65+
func TestAccSpannerInstance_spannerInstanceProcessingUnitsExample(t *testing.T) {
66+
skipIfVcr(t)
67+
t.Parallel()
68+
69+
context := map[string]interface{}{
70+
"random_suffix": randString(t, 10),
71+
}
72+
73+
vcrTest(t, resource.TestCase{
74+
PreCheck: func() { testAccPreCheck(t) },
75+
Providers: testAccProviders,
76+
CheckDestroy: testAccCheckSpannerInstanceDestroyProducer(t),
77+
Steps: []resource.TestStep{
78+
{
79+
Config: testAccSpannerInstance_spannerInstanceProcessingUnitsExample(context),
80+
},
81+
{
82+
ResourceName: "google_spanner_instance.example",
83+
ImportState: true,
84+
ImportStateVerify: true,
85+
ImportStateVerifyIgnore: []string{"config"},
86+
},
87+
},
88+
})
89+
}
90+
91+
func testAccSpannerInstance_spannerInstanceProcessingUnitsExample(context map[string]interface{}) string {
92+
return Nprintf(`
93+
resource "google_spanner_instance" "example" {
94+
config = "regional-us-central1"
95+
display_name = "Test Spanner Instance"
96+
processing_units = 200
97+
labels = {
98+
"foo" = "bar"
99+
}
100+
}
101+
`, context)
102+
}
103+
65104
func TestAccSpannerInstance_spannerInstanceMultiRegionalExample(t *testing.T) {
66105
skipIfVcr(t)
67106
t.Parallel()

google-beta/resource_spanner_instance_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ func TestAccSpannerInstance_basic(t *testing.T) {
7171
})
7272
}
7373

74+
func TestAccSpannerInstance_noNodeCountSpecified(t *testing.T) {
75+
t.Parallel()
76+
77+
idName := fmt.Sprintf("spanner-test-%s", randString(t, 10))
78+
vcrTest(t, resource.TestCase{
79+
PreCheck: func() { testAccPreCheck(t) },
80+
Providers: testAccProviders,
81+
CheckDestroy: testAccCheckSpannerInstanceDestroyProducer(t),
82+
Steps: []resource.TestStep{
83+
{
84+
Config: testAccSpannerInstance_noNodeCountSpecified(idName),
85+
Check: resource.ComposeTestCheckFunc(
86+
resource.TestCheckResourceAttrSet("google_spanner_instance.basic", "state"),
87+
),
88+
},
89+
{
90+
ResourceName: "google_spanner_instance.basic",
91+
ImportState: true,
92+
ImportStateVerify: true,
93+
},
94+
},
95+
})
96+
}
97+
7498
func TestAccSpannerInstance_basicWithAutogenName(t *testing.T) {
7599
// Randomness
76100
skipIfVcr(t)
@@ -140,6 +164,16 @@ resource "google_spanner_instance" "basic" {
140164
`, name, name)
141165
}
142166

167+
func testAccSpannerInstance_noNodeCountSpecified(name string) string {
168+
return fmt.Sprintf(`
169+
resource "google_spanner_instance" "basic" {
170+
name = "%s"
171+
config = "regional-us-central1"
172+
display_name = "%s-dname"
173+
}
174+
`, name, name)
175+
}
176+
143177
func testAccSpannerInstance_basicWithAutogenName(name string) string {
144178
return fmt.Sprintf(`
145179
resource "google_spanner_instance" "basic" {

website/docs/r/spanner_instance.html.markdown

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ resource "google_spanner_instance" "example" {
5151
}
5252
}
5353
```
54+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
55+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=spanner_instance_processing_units&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
56+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
57+
</a>
58+
</div>
59+
## Example Usage - Spanner Instance Processing Units
60+
61+
62+
```hcl
63+
resource "google_spanner_instance" "example" {
64+
config = "regional-us-central1"
65+
display_name = "Test Spanner Instance"
66+
processing_units = 200
67+
labels = {
68+
"foo" = "bar"
69+
}
70+
}
71+
```
5472
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
5573
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=spanner_instance_multi_regional&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
5674
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
@@ -103,7 +121,13 @@ The following arguments are supported:
103121

104122
* `num_nodes` -
105123
(Optional)
106-
The number of nodes allocated to this instance.
124+
The number of nodes allocated to this instance. At most one of either node_count or processing_units
125+
can be present in terraform.
126+
127+
* `processing_units` -
128+
(Optional)
129+
The number of processing units allocated to this instance. At most one of processing_units
130+
or node_count can be present in terraform.
107131

108132
* `labels` -
109133
(Optional)

0 commit comments

Comments
 (0)