Skip to content

Commit

Permalink
[endpoint]: change the resource ID format in ccloud_endpoint_accept_v…
Browse files Browse the repository at this point in the history
…1 resource
  • Loading branch information
kayrus committed Jul 18, 2024
1 parent 07d530c commit e0c6b6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
39 changes: 15 additions & 24 deletions ccloud/resource_ccloud_endpoint_accept_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/go-openapi/strfmt"
Expand All @@ -21,7 +20,7 @@ func resourceCCloudEndpointAcceptV1() *schema.Resource {
ReadContext: resourceCCloudEndpointAcceptV1Read,
DeleteContext: resourceCCloudEndpointAcceptV1Delete,
Importer: &schema.ResourceImporter{
StateContext: archerSetServiceEndpointConsumerImport,
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -81,7 +80,8 @@ func resourceCCloudEndpointAcceptV1Create(ctx context.Context, d *schema.Resourc

log.Printf("[DEBUG] Accepted Archer endpoint: %v", res)

d.SetId(endpointID)
id := fmt.Sprintf("%s/%s", serviceID, endpointID)
d.SetId(id)

// waiting for AVAILABLE status
timeout := d.Timeout(schema.TimeoutCreate)
Expand Down Expand Up @@ -109,8 +109,11 @@ func resourceCCloudEndpointAcceptV1Read(ctx context.Context, d *schema.ResourceD
return diag.Errorf("error creating Archer client: %s", err)
}

id := d.Id()
serviceID := d.Get("service_id").(string)
serviceID, id, err := parsePairedIDs(d.Id(), "ccloud_endpoint_accept_v1")
if err != nil {
return diag.FromErr(err)
}

ec, err := archerGetServiceEndpointConsumer(ctx, c, id, serviceID)
if err != nil {
if _, ok := err.(*service.GetServiceServiceIDEndpointsNotFound); ok {
Expand All @@ -133,9 +136,13 @@ func resourceCCloudEndpointAcceptV1Delete(ctx context.Context, d *schema.Resourc
}
client := c.Service

serviceID := d.Get("service_id").(string)
serviceID, id, err := parsePairedIDs(d.Id(), "ccloud_endpoint_accept_v1")
if err != nil {
return diag.FromErr(err)
}

req := &models.EndpointConsumerList{
EndpointIds: []strfmt.UUID{strfmt.UUID(d.Id())},
EndpointIds: []strfmt.UUID{strfmt.UUID(id)},
}
opts := &service.PutServiceServiceIDRejectEndpointsParams{
Body: req,
Expand All @@ -160,7 +167,7 @@ func resourceCCloudEndpointAcceptV1Delete(ctx context.Context, d *schema.Resourc
string(models.EndpointStatusPENDINGREJECTED),
string(models.EndpointStatusPENDINGDELETE),
}
_, err = archerWaitForServiceEndpointConsumer(ctx, c, d.Id(), serviceID, target, pending, timeout)
_, err = archerWaitForServiceEndpointConsumer(ctx, c, id, serviceID, target, pending, timeout)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -228,19 +235,3 @@ func archerSetServiceEndpointConsumer(d *schema.ResourceData, config *Config, id
d.Set("status", consumer.Status)
d.Set("region", GetRegion(d, config))
}

func archerSetServiceEndpointConsumerImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 3 {
err := fmt.Errorf("Invalid format specified for endpoint consumer. Format must be <endpoint id>/<service id>")
return nil, err
}

id := parts[0]
serviceID := parts[1]

d.SetId(id)
d.Set("service_id", serviceID)

return []*schema.ResourceData{d}, nil
}
12 changes: 12 additions & 0 deletions ccloud/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,15 @@ func ptrValue[T any](p *T) T {
var t T
return t
}

// parsePairedIDs is a helper function that parses a raw ID into two
// separate IDs. This is useful for resources that have a parent/child
// relationship.
func parsePairedIDs(id string, res string) (string, string, error) {
parts := strings.SplitN(id, "/", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("Unable to determine %s ID from raw ID: %s", res, id)
}

return parts[0], parts[1], nil
}
2 changes: 1 addition & 1 deletion website/docs/r/endpoint_accept_v1.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The following arguments are supported:

In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the Archer endpoint.
* `id` - A combined ID of the Archer service and endpoint separated by a slash.
* `status` - The current status of the Archer service endpoint acceptance.

## Import
Expand Down

0 comments on commit e0c6b6b

Please sign in to comment.