Skip to content

Commit

Permalink
Merge pull request #70 from warpstreamlabs/simon/warp-964-expose-appl…
Browse files Browse the repository at this point in the history
…ication-bootstrap-url-via-the-api-and-terraform

Include bootstrap url in virtual cluster resource and data sources
  • Loading branch information
7imon7ays authored Jul 23, 2024
2 parents 89406cd + fc87ee8 commit 8aee21d
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/data-sources/virtual_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ output "vc_default_id" {
- `agent_keys` (Attributes List) List of keys to authenticate an agent with this cluster. Null for Serverless clusters. (see [below for nested schema](#nestedatt--agent_keys))
- `agent_pool_id` (String)
- `agent_pool_name` (String)
- `bootstrap_url` (String) Bootstrap URL to connect to the Virtual Cluster. Null for Serverless clusters.
- `cloud` (Attributes) (see [below for nested schema](#nestedatt--cloud))
- `configuration` (Attributes) (see [below for nested schema](#nestedatt--configuration))
- `created_at` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/virtual_clusters.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Read-Only:
- `agent_keys` (Attributes List) List of keys to authenticate an agent with this cluster. Null for Serverless clusters. (see [below for nested schema](#nestedatt--virtual_clusters--agent_keys))
- `agent_pool_id` (String)
- `agent_pool_name` (String)
- `bootstrap_url` (String) Bootstrap URL to connect to the Virtual Cluster. Null for Serverless clusters.
- `created_at` (String)
- `id` (String)
- `name` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/resources/virtual_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ resource "warpstream_virtual_cluster" "test_cloud_region" {
- `agent_keys` (Attributes List) List of keys to authenticate an agent with this cluster. Null for Serverless clusters. (see [below for nested schema](#nestedatt--agent_keys))
- `agent_pool_id` (String) Agent Pool ID.
- `agent_pool_name` (String) Agent Pool Name.
- `bootstrap_url` (String) Bootstrap URL to connect to the Virtual Cluster. Null for Serverless clusters.
- `created_at` (String) Virtual Cluster Creation Timestamp.
- `default` (Boolean)
- `id` (String) Virtual Cluster ID.
Expand Down
1 change: 1 addition & 0 deletions internal/provider/api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type VirtualCluster struct {
CreatedAt string `json:"created_at"`
Region string `json:"region"`
CloudProvider string `json:"cloud_provider"`
BootstrapURL *string `json:"bootstrap_url"`
}

type VirtualClusterCredentials struct {
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/utils/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ func TestCheckResourceAttrStartsWith(pathToAttr, attr, prefix string) resource.T
},
)
}

func TestCheckResourceAttrEndsWith(pathToAttr, attr, suffix string) resource.TestCheckFunc {
return resource.TestCheckResourceAttrWith(
pathToAttr,
attr,
func(value string) error {
if !strings.HasSuffix(value, suffix) {
return fmt.Errorf("expected %s to end with '%s', got: %s", attr, suffix, value)
}
return nil
},
)
}
2 changes: 2 additions & 0 deletions internal/provider/virtual_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type virtualClusterDataSourceModel struct {
Default types.Bool `tfsdk:"default"`
Configuration types.Object `tfsdk:"configuration"`
Cloud types.Object `tfsdk:"cloud"`
BootstrapURL types.String `tfsdk:"bootstrap_url"`
}

type virtualClusterResourceModel struct {
Expand All @@ -35,6 +36,7 @@ type virtualClusterResourceModel struct {
Default types.Bool `tfsdk:"default"`
Configuration types.Object `tfsdk:"configuration"`
Cloud types.Object `tfsdk:"cloud"`
BootstrapURL types.String `tfsdk:"bootstrap_url"`
}

type virtualClusterConfigurationModel struct {
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/virtual_cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (d *virtualClusterDataSource) Schema(_ context.Context, _ datasource.Schema
},
Computed: true,
},
"bootstrap_url": schema.StringAttribute{
Description: "Bootstrap URL to connect to the Virtual Cluster. Null for Serverless clusters.",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -176,6 +180,10 @@ func (d *virtualClusterDataSource) Read(ctx context.Context, req datasource.Read
Cloud: data.Cloud,
}

if vc.BootstrapURL != nil {
state.BootstrapURL = types.StringValue(*vc.BootstrapURL)
}

// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/virtual_cluster_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func testAccVCDataSourceCheck_byoc(name string) resource.TestCheckFunc {
resource.TestCheckResourceAttr(
"data.warpstream_virtual_cluster.test", "agent_keys.0.name", "akn_virtual_cluster_default_7695dba1efaa",
),
utils.TestCheckResourceAttrEndsWith(
"data.warpstream_virtual_cluster.test",
"bootstrap_url",
".kafka.discoveryv2.prod-z.us-east-1.warpstream.com:9092",
),
testAccVCDataSourceCheck(name),
)
}
Expand Down
19 changes: 19 additions & 0 deletions internal/provider/virtual_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ This resource allows you to create, update and delete virtual clusters.
objectplanmodifier.RequiresReplace(),
},
},
"bootstrap_url": schema.StringAttribute{
Description: "Bootstrap URL to connect to the Virtual Cluster. Null for Serverless clusters.",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -286,6 +290,10 @@ func (r *virtualClusterResource) Create(ctx context.Context, req resource.Create
Cloud: plan.Cloud,
}

if cluster.BootstrapURL != nil {
state.BootstrapURL = types.StringValue(*cluster.BootstrapURL)
}

// Set state to fully populated data
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -334,6 +342,10 @@ func (r *virtualClusterResource) Read(ctx context.Context, req resource.ReadRequ
state.CreatedAt = types.StringValue(cluster.CreatedAt)
state.Default = types.BoolValue(cluster.Name == "vcn_default")

if cluster.BootstrapURL != nil {
state.BootstrapURL = types.StringValue(*cluster.BootstrapURL)
}

cloudValue, diagnostics := types.ObjectValue(
virtualClusterCloudModel{}.AttributeTypes(),
map[string]attr.Value{
Expand Down Expand Up @@ -409,13 +421,20 @@ func (r *virtualClusterResource) ImportState(ctx context.Context, req resource.I
}

func (m virtualClusterResourceModel) cluster() api.VirtualCluster {
var burl *string
if m.BootstrapURL.ValueString() != "" {
burlStr := m.BootstrapURL.ValueString()
burl = &burlStr
}

return api.VirtualCluster{
ID: m.ID.ValueString(),
Name: m.Name.ValueString(),
Type: m.Type.ValueString(),
AgentPoolID: m.AgentPoolID.ValueString(),
AgentPoolName: m.AgentPoolName.ValueString(),
CreatedAt: m.CreatedAt.ValueString(),
BootstrapURL: burl,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/provider/virtual_cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,22 @@ func testAccVirtualClusterResourceCheck_BYOC(acls bool, autoTopic bool, numParts
testAccVirtualClusterResourceCheck(acls, autoTopic, numParts, "byoc"),
resource.TestCheckResourceAttr("warpstream_virtual_cluster.test", "agent_keys.#", "1"),
utils.TestCheckResourceAttrStartsWith("warpstream_virtual_cluster.test", "agent_keys.0.name", "akn_virtual_cluster_test_acc_"),
utils.TestCheckResourceAttrEndsWith("warpstream_virtual_cluster.test", "bootstrap_url", ".kafka.discoveryv2.prod-z.us-east-1.warpstream.com:9092"),
)
}

func testAccVirtualClusterResourceCheck_Serverless(acls bool, autoTopic bool, numParts int64) resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc(
testAccVirtualClusterResourceCheck(acls, autoTopic, numParts, "serverless"),
resource.TestCheckNoResourceAttr("warpstream_virtual_cluster.test", "agent_keys"),
resource.TestCheckNoResourceAttr("warpstream_virtual_cluster.test", "bootstrap_url"),
)
}

func testAccVirtualClusterResourceCheck(acls bool, autoTopic bool, numParts int64, vcType string) resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("warpstream_virtual_cluster.test", "id"),
resource.TestCheckResourceAttrSet("warpstream_virtual_cluster.test", "agent_pool_id"),
resource.TestCheckResourceAttrSet("warpstream_virtual_cluster.test", "agent_pool_id"),
resource.TestCheckResourceAttrSet("warpstream_virtual_cluster.test", "created_at"),
// Note: agent_pool_name is now equal to "apn_test_acc_"+nameSuffix + randomSuffix
resource.TestCheckResourceAttrSet("warpstream_virtual_cluster.test", "agent_pool_name"),
Expand Down
9 changes: 9 additions & 0 deletions internal/provider/virtual_clusters_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type virtualClustersModel struct {
AgentPoolID types.String `tfsdk:"agent_pool_id"`
AgentPoolName types.String `tfsdk:"agent_pool_name"`
CreatedAt types.String `tfsdk:"created_at"`
BootstrapURL types.String `tfsdk:"bootstrap_url"`
}

// Metadata returns the data source type name.
Expand Down Expand Up @@ -75,6 +76,10 @@ func (d *virtualClustersDataSource) Schema(_ context.Context, _ datasource.Schem
"agent_pool_name": schema.StringAttribute{
Computed: true,
},
"bootstrap_url": schema.StringAttribute{
Description: "Bootstrap URL to connect to the Virtual Cluster. Null for Serverless clusters.",
Computed: true,
},
"created_at": schema.StringAttribute{
Computed: true,
},
Expand Down Expand Up @@ -113,6 +118,10 @@ func (d *virtualClustersDataSource) Read(ctx context.Context, req datasource.Rea
CreatedAt: types.StringValue(vcn.CreatedAt),
}

if vcn.BootstrapURL != nil {
vcnState.BootstrapURL = types.StringValue(*vcn.BootstrapURL)
}

state.VirtualClusters = append(state.VirtualClusters, vcnState)

diags := resp.State.Set(ctx, &state)
Expand Down
20 changes: 20 additions & 0 deletions internal/provider/virtual_clusters_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ func assertBYOCVC(vcs []map[string]string, name string) error {
return fmt.Errorf("Expected agent key virtual cluster ID to be 'vci_daf191a9_47fa_4215_aa49_2e74c5ba78d9', got %s", agentKeysVCIDAttr)
}

burl, ok := vc["bootstrap_url"]

if !ok {
return fmt.Errorf("Expected byoc virtual cluster JSON to have a bootstrap URL field")
}

expectedBurl := "api-18b7339a-6bc0-4397-a066-d3bddb4f8392.kafka.discoveryv2.prod-z.us-east-1.warpstream.com:9092"

if burl != expectedBurl {
return fmt.Errorf(
"Expected vcn_wtf byoc cluster bootstrap URL to be %s, got %s",
expectedBurl,
vc["bootstrap_url"],
)
}

return nil
}

Expand All @@ -126,6 +142,10 @@ func assertServerlessVC(vcs []map[string]string, name string) error {
)
}

if _, ok := vc["bootstrap_url"]; ok {
return fmt.Errorf("Serverless virtual cluster should not have a bootstrap URL field")
}

return nil
}

Expand Down

0 comments on commit 8aee21d

Please sign in to comment.