Skip to content

Commit

Permalink
Move resources to the service packages (#8126) (#14888)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 12, 2023
1 parent 8903576 commit cef5a5a
Show file tree
Hide file tree
Showing 61 changed files with 2,916 additions and 2,770 deletions.
Empty file added .changelog/.txt
Empty file.
208 changes: 3 additions & 205 deletions google/data_source_google_compute_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,212 +3,10 @@
package google

import (
"fmt"
"regexp"
"strings"

"github.com/hashicorp/terraform-provider-google/google/tpgresource"
"github.com/hashicorp/terraform-provider-google/google/services/compute"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

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

var (
computeAddressIdTemplate = "projects/%s/regions/%s/addresses/%s"
computeAddressLinkRegex = regexp.MustCompile("projects/(.+)/regions/(.+)/addresses/(.+)$")
)

func DataSourceGoogleComputeAddress() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleComputeAddressRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"address": {
Type: schema.TypeString,
Computed: true,
},

"address_type": {
Type: schema.TypeString,
Computed: true,
},

"network": {
Type: schema.TypeString,
Computed: true,
},

"network_tier": {
Type: schema.TypeString,
Computed: true,
},

"prefix_length": {
Type: schema.TypeInt,
Computed: true,
},

"purpose": {
Type: schema.TypeString,
Computed: true,
},

"subnetwork": {
Type: schema.TypeString,
Computed: true,
},

"users": {
Type: schema.TypeString,
Computed: true,
},

"status": {
Type: schema.TypeString,
Computed: true,
},

"self_link": {
Type: schema.TypeString,
Computed: true,
},

"region": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},

"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
}

func dataSourceGoogleComputeAddressRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
region, err := tpgresource.GetRegion(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)

address, err := config.NewComputeClient(userAgent).Addresses.Get(project, region, name).Do()
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Address Not Found : %s", name))
}

if err := d.Set("address", address.Address); err != nil {
return fmt.Errorf("Error setting address: %s", err)
}
if err := d.Set("address_type", address.AddressType); err != nil {
return fmt.Errorf("Error setting address_type: %s", err)
}
if err := d.Set("network", address.Network); err != nil {
return fmt.Errorf("Error setting network: %s", err)
}
if err := d.Set("network_tier", address.NetworkTier); err != nil {
return fmt.Errorf("Error setting network_tier: %s", err)
}
if err := d.Set("prefix_length", address.PrefixLength); err != nil {
return fmt.Errorf("Error setting prefix_length: %s", err)
}
if err := d.Set("purpose", address.Purpose); err != nil {
return fmt.Errorf("Error setting purpose: %s", err)
}
if err := d.Set("subnetwork", address.Subnetwork); err != nil {
return fmt.Errorf("Error setting subnetwork: %s", err)
}
if err := d.Set("status", address.Status); err != nil {
return fmt.Errorf("Error setting status: %s", err)
}
if err := d.Set("self_link", address.SelfLink); err != nil {
return fmt.Errorf("Error setting self_link: %s", err)
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("region", region); err != nil {
return fmt.Errorf("Error setting region: %s", err)
}

d.SetId(fmt.Sprintf("projects/%s/regions/%s/addresses/%s", project, region, name))
return nil
}

type computeAddressId struct {
Project string
Region string
Name string
}

func (s computeAddressId) canonicalId() string {
return fmt.Sprintf(computeAddressIdTemplate, s.Project, s.Region, s.Name)
}

func parseComputeAddressId(id string, config *transport_tpg.Config) (*computeAddressId, error) {
var parts []string
if computeAddressLinkRegex.MatchString(id) {
parts = computeAddressLinkRegex.FindStringSubmatch(id)

return &computeAddressId{
Project: parts[1],
Region: parts[2],
Name: parts[3],
}, nil
} else {
parts = strings.Split(id, "/")
}

if len(parts) == 3 {
return &computeAddressId{
Project: parts[0],
Region: parts[1],
Name: parts[2],
}, nil
} else if len(parts) == 2 {
// Project is optional.
if config.Project == "" {
return nil, fmt.Errorf("The default project for the provider must be set when using the `{region}/{name}` id format.")
}

return &computeAddressId{
Project: config.Project,
Region: parts[0],
Name: parts[1],
}, nil
} else if len(parts) == 1 {
// Project and region is optional
if config.Project == "" {
return nil, fmt.Errorf("The default project for the provider must be set when using the `{name}` id format.")
}
if config.Region == "" {
return nil, fmt.Errorf("The default region for the provider must be set when using the `{name}` id format.")
}

return &computeAddressId{
Project: config.Project,
Region: config.Region,
Name: parts[0],
}, nil
}

return nil, fmt.Errorf("Invalid compute address id. Expecting resource link, `{project}/{region}/{name}`, `{region}/{name}` or `{name}` format.")
func parseComputeAddressId(id string, config *transport_tpg.Config) (*compute.ComputeAddressId, error) {
return compute.ParseComputeAddressId(id, config)
}
9 changes: 5 additions & 4 deletions google/data_source_google_compute_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/hashicorp/terraform-provider-google/google/services/compute"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

Expand Down Expand Up @@ -56,7 +57,7 @@ func TestComputeAddressIdParsing(t *testing.T) {
}

for tn, tc := range cases {
addressId, err := parseComputeAddressId(tc.ImportId, tc.Config)
addressId, err := compute.ParseComputeAddressId(tc.ImportId, tc.Config)

if tc.ExpectedError && err == nil {
t.Fatalf("bad: %s, expected an error", tn)
Expand All @@ -69,8 +70,8 @@ func TestComputeAddressIdParsing(t *testing.T) {
t.Fatalf("bad: %s, err: %#v", tn, err)
}

if addressId.canonicalId() != tc.ExpectedCanonicalId {
t.Fatalf("bad: %s, expected canonical id to be `%s` but is `%s`", tn, tc.ExpectedCanonicalId, addressId.canonicalId())
if addressId.CanonicalId() != tc.ExpectedCanonicalId {
t.Fatalf("bad: %s, expected canonical id to be `%s` but is `%s`", tn, tc.ExpectedCanonicalId, addressId.CanonicalId())
}
}
}
Expand Down Expand Up @@ -156,7 +157,7 @@ func testAccCheckDataSourceComputeAddressDestroy(t *testing.T, name string) reso

config := GoogleProviderConfig(t)

addressId, err := parseComputeAddressId(rs.Primary.ID, nil)
addressId, err := compute.ParseComputeAddressId(rs.Primary.ID, nil)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit cef5a5a

Please sign in to comment.