Skip to content

Commit

Permalink
Add Pub/Sub Lite reservation support to terraform. (#5284) (#794)
Browse files Browse the repository at this point in the history
* Add Pub/Sub Lite reservation support to terraform.

Also fix doc links for Pub/Sub Lite.

* Add Pub/Sub Lite reservation support to terraform.

Also fix doc links for Pub/Sub Lite.

* Add Pub/Sub Lite reservation support to terraform.

Also fix doc links for Pub/Sub Lite.

* Add Pub/Sub Lite reservation support to terraform.

Also fix doc links for Pub/Sub Lite.

* Update to expander to use 'locations' instead of 'regions'.

Co-authored-by: Nathan Mckinley <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Nathan Mckinley <[email protected]>
  • Loading branch information
modular-magician and nat-henderson authored Oct 6, 2021
1 parent e8ae8fe commit 20190b3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
63 changes: 63 additions & 0 deletions google/pubsublite_reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// 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 "reflect"

const PubsubLiteReservationAssetType string = "{{region}}-pubsublite.googleapis.com/Reservation"

func resourceConverterPubsubLiteReservation() ResourceConverter {
return ResourceConverter{
AssetType: PubsubLiteReservationAssetType,
Convert: GetPubsubLiteReservationCaiObject,
}
}

func GetPubsubLiteReservationCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
name, err := assetName(d, config, "//{{region}}-pubsublite.googleapis.com/projects/{{project}}/locations/{{region}}/reservations/{{name}}")
if err != nil {
return []Asset{}, err
}
if obj, err := GetPubsubLiteReservationApiObject(d, config); err == nil {
return []Asset{{
Name: name,
Type: PubsubLiteReservationAssetType,
Resource: &AssetResource{
Version: "admin",
DiscoveryDocumentURI: "https://www.googleapis.com/discovery/v1/apis/{{region}}-pubsublite/admin/rest",
DiscoveryName: "Reservation",
Data: obj,
},
}}, nil
} else {
return []Asset{}, err
}
}

func GetPubsubLiteReservationApiObject(d TerraformResourceData, config *Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})
throughputCapacityProp, err := expandPubsubLiteReservationThroughputCapacity(d.Get("throughput_capacity"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("throughput_capacity"); !isEmptyValue(reflect.ValueOf(throughputCapacityProp)) && (ok || !reflect.DeepEqual(v, throughputCapacityProp)) {
obj["throughputCapacity"] = throughputCapacityProp
}

return obj, nil
}

func expandPubsubLiteReservationThroughputCapacity(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
34 changes: 34 additions & 0 deletions google/pubsublite_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func GetPubsubLiteTopicApiObject(d TerraformResourceData, config *Config) (map[s
} else if v, ok := d.GetOkExists("retention_config"); !isEmptyValue(reflect.ValueOf(retentionConfigProp)) && (ok || !reflect.DeepEqual(v, retentionConfigProp)) {
obj["retentionConfig"] = retentionConfigProp
}
reservationConfigProp, err := expandPubsubLiteTopicReservationConfig(d.Get("reservation_config"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("reservation_config"); !isEmptyValue(reflect.ValueOf(reservationConfigProp)) && (ok || !reflect.DeepEqual(v, reservationConfigProp)) {
obj["reservationConfig"] = reservationConfigProp
}

return resourcePubsubLiteTopicEncoder(d, config, obj)
}
Expand Down Expand Up @@ -187,3 +193,31 @@ func expandPubsubLiteTopicRetentionConfigPerPartitionBytes(v interface{}, d Terr
func expandPubsubLiteTopicRetentionConfigPeriod(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandPubsubLiteTopicReservationConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedThroughputReservation, err := expandPubsubLiteTopicReservationConfigThroughputReservation(original["throughput_reservation"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedThroughputReservation); val.IsValid() && !isEmptyValue(val) {
transformed["throughputReservation"] = transformedThroughputReservation
}

return transformed, nil
}

func expandPubsubLiteTopicReservationConfigThroughputReservation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseRegionalFieldValue("reservations", v.(string), "project", "region", "zone", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for throughput_reservation: %s", err)
}
// Custom due to "locations" rather than "regions".
return fmt.Sprintf("projects/%s/locations/%s/reservations/%s", f.Project, f.Region, f.Name), nil
}

0 comments on commit 20190b3

Please sign in to comment.