Skip to content

Commit 04e8ad4

Browse files
authored
feat: support proton cluster (#7)
1 parent 3795b8c commit 04e8ad4

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

internal/provider/provider.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ type TimeplusProviderModel struct {
3232
Endpoint types.String `tfsdk:"endpoint"`
3333
Workspace types.String `tfsdk:"workspace"`
3434
ApiKey types.String `tfsdk:"api_key"`
35+
36+
// Ideally we should read this from stream definitions. However, there are 2 limitations
37+
// 1. Proton cluster (e.g. replica = 3) doesn't allow stream with relicatoin_refactor equals other number (e.g. 2)
38+
// 2. Proton get/list stream endpoint doesn't return relicatoin_refactor of the stream
39+
// Thus, we currently define this `replicas` as a provider setting
40+
Replicas types.Int64 `tfsdk:"replicas"`
3541
}
3642

3743
func (p *TimeplusProvider) Metadata(ctx context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
@@ -59,6 +65,11 @@ Use the navigation to the left to read about the available resources.`,
5965
Required: true,
6066
Sensitive: true,
6167
},
68+
"replicas": schema.Int64Attribute{
69+
MarkdownDescription: "Number of Proton replicas",
70+
Required: false,
71+
Optional: true,
72+
},
6273
},
6374
}
6475
}
@@ -72,8 +83,14 @@ func (p *TimeplusProvider) Configure(ctx context.Context, req provider.Configure
7283
return
7384
}
7485

86+
var replicas *int
87+
if !(data.Replicas.IsNull() || data.Replicas.IsUnknown()) {
88+
valInt := int(*data.Replicas.ValueInt64Pointer())
89+
replicas = &valInt
90+
}
91+
7592
// Configuration values are now available.
76-
client, err := timeplus.NewClient(data.Workspace.ValueString(), data.ApiKey.ValueString(), timeplus.ClientOptions{
93+
client, err := timeplus.NewClient(data.Workspace.ValueString(), data.ApiKey.ValueString(), replicas, timeplus.ClientOptions{
7794
BaseURL: data.Endpoint.ValueString(),
7895
})
7996
if err != nil {

internal/timeplus/client.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ type resource interface {
1919
type Client struct {
2020
*http.Client
2121

22-
baseURL *url.URL
23-
apiKey string
22+
baseURL *url.URL
23+
apiKey string
24+
replicas *int
2425
}
2526

2627
// optional configurations for the client
@@ -40,7 +41,7 @@ func DefaultOptions() ClientOptions {
4041
}
4142
}
4243

43-
func NewClient(workspaceID string, apiKey string, opts ClientOptions) (*Client, error) {
44+
func NewClient(workspaceID string, apiKey string, replicas *int, opts ClientOptions) (*Client, error) {
4445
ops := DefaultOptions()
4546
ops.merge(opts)
4647

@@ -51,9 +52,10 @@ func NewClient(workspaceID string, apiKey string, opts ClientOptions) (*Client,
5152
baseURL = baseURL.JoinPath(workspaceID, "api", "v1beta2")
5253

5354
return &Client{
54-
Client: http.DefaultClient,
55-
baseURL: baseURL,
56-
apiKey: apiKey,
55+
Client: http.DefaultClient,
56+
baseURL: baseURL,
57+
apiKey: apiKey,
58+
replicas: replicas,
5759
}, nil
5860
}
5961

internal/timeplus/stream.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ func (Stream) resourcePath() string {
8484
}
8585

8686
func (c *Client) CreateStream(s *Stream) error {
87+
if c.replicas != nil {
88+
s.ReplicationFactor = *c.replicas
89+
}
90+
8791
if err := c.post(s); err != nil {
8892
return err
8993
}

0 commit comments

Comments
 (0)