Skip to content

Commit 4cf255b

Browse files
authored
Profiles warehouse resource (#59)
1 parent 76ad5c6 commit 4cf255b

22 files changed

+759
-42
lines changed

docs/resources/profiles_warehouse.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "segment_profiles_warehouse Resource - terraform-provider-segment"
4+
subcategory: ""
5+
description: |-
6+
A Profiles Sync Warehouse is a central repository of data collected from your workspace. It is what commonly comes to mind when you think about a relational database: structured data that fits into rows and columns.
7+
To import a Profiles Warehouse into Terraform, use the following format: 'space-id:warehouse-id'
8+
---
9+
10+
# segment_profiles_warehouse (Resource)
11+
12+
A Profiles Sync Warehouse is a central repository of data collected from your workspace. It is what commonly comes to mind when you think about a relational database: structured data that fits into rows and columns.
13+
14+
To import a Profiles Warehouse into Terraform, use the following format: 'space-id:warehouse-id'
15+
16+
## Example Usage
17+
18+
```terraform
19+
# Configures a specific profiles sync warehouse
20+
resource "segment_profiles_warehouse" "example" {
21+
space_id = "cba321"
22+
metadata_id = "abc123"
23+
enabled = true
24+
settings = jsonencode({
25+
token : "zyx321"
26+
})
27+
name = "My Terraform Warehouse!"
28+
}
29+
```
30+
31+
<!-- schema generated by tfplugindocs -->
32+
## Schema
33+
34+
### Required
35+
36+
- `metadata_id` (String) The Warehouse metadata to use.
37+
- `settings` (String) A key-value object that contains instance-specific settings for a Warehouse.
38+
39+
Different kinds of Warehouses require different settings. The required and optional settings
40+
for a Warehouse are described in the 'options' object of the associated Warehouse metadata.
41+
42+
You can find the full list of Warehouse metadata and related settings information in the
43+
'/catalog/warehouses' endpoint.
44+
- `space_id` (String) The Space id.
45+
46+
### Optional
47+
48+
- `enabled` (Boolean) Enable to allow this Warehouse to receive data. Defaults to true.
49+
- `name` (String) An optional human-readable name for this Warehouse.
50+
- `schema_name` (String) The custom schema name that Segment uses on the Warehouse side. The space slug value is default otherwise.
51+
52+
### Read-Only
53+
54+
- `id` (String) The id of the Warehouse.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Configures a specific profiles sync warehouse
2+
resource "segment_profiles_warehouse" "example" {
3+
space_id = "cba321"
4+
metadata_id = "abc123"
5+
enabled = true
6+
settings = jsonencode({
7+
token : "zyx321"
8+
})
9+
name = "My Terraform Warehouse!"
10+
}

internal/provider/destination_data_source.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (d *destinationDataSource) Read(ctx context.Context, req datasource.ReadReq
8181
}
8282
if err != nil {
8383
resp.Diagnostics.AddError(
84-
"Unable to Read Destination",
84+
"Unable to read Destination",
8585
getError(err, body),
8686
)
8787

@@ -93,7 +93,7 @@ func (d *destinationDataSource) Read(ctx context.Context, req datasource.ReadReq
9393
err = state.Fill(&destination)
9494
if err != nil {
9595
resp.Diagnostics.AddError(
96-
"Unable to Read Destination",
96+
"Unable to populate Destination state",
9797
err.Error(),
9898
)
9999

internal/provider/destination_filter_resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func (r *destinationFilterResource) Schema(_ context.Context, _ resource.SchemaR
5454
"id": schema.StringAttribute{
5555
Computed: true,
5656
Description: "The unique id of this filter.",
57+
PlanModifiers: []planmodifier.String{
58+
stringplanmodifier.UseStateForUnknown(),
59+
},
5760
},
5861
"if": schema.StringAttribute{
5962
Required: true,

internal/provider/destination_metadata_data_source.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (d *destinationMetadataDataSource) Read(ctx context.Context, req datasource
401401
}
402402
if err != nil {
403403
resp.Diagnostics.AddError(
404-
"Unable to Read Source metadata",
404+
"Unable to read Source metadata",
405405
getError(err, body),
406406
)
407407

@@ -412,7 +412,7 @@ func (d *destinationMetadataDataSource) Read(ctx context.Context, req datasource
412412
err = state.Fill(destinationMetadata)
413413
if err != nil {
414414
resp.Diagnostics.AddError(
415-
"Unable to Read Source metadata",
415+
"Unable to read Source metadata",
416416
err.Error(),
417417
)
418418

internal/provider/destination_resource.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ func (r *destinationResource) Create(ctx context.Context, req resource.CreateReq
554554
err = state.Fill(&destination)
555555
if err != nil {
556556
resp.Diagnostics.AddError(
557-
"Unable to create Destination",
557+
"Unable to populate Destination state",
558558
err.Error(),
559559
)
560560

@@ -600,7 +600,7 @@ func (r *destinationResource) Read(ctx context.Context, req resource.ReadRequest
600600
err = state.Fill(&destination)
601601
if err != nil {
602602
resp.Diagnostics.AddError(
603-
"Unable to read Destination",
603+
"Unable to populate Destination state",
604604
err.Error(),
605605
)
606606

@@ -662,7 +662,7 @@ func (r *destinationResource) Update(ctx context.Context, req resource.UpdateReq
662662
err = state.Fill(&destination)
663663
if err != nil {
664664
resp.Diagnostics.AddError(
665-
"Unable to update Destination",
665+
"Unable to populate Destination state",
666666
err.Error(),
667667
)
668668

@@ -696,7 +696,8 @@ func (r *destinationResource) Delete(ctx context.Context, req resource.DeleteReq
696696
}
697697
if err != nil {
698698
resp.Diagnostics.AddError(
699-
"Error deleting Destination", "Could not delete Destination, unexpected error: "+getError(err, body),
699+
"Unable to delete Destination",
700+
getError(err, body),
700701
)
701702

702703
return

internal/provider/label_resource.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,16 @@ func (r *labelResource) Create(ctx context.Context, req resource.CreateRequest,
107107
}
108108
if err != nil {
109109
resp.Diagnostics.AddError(
110-
"Unable to create a label",
110+
"Unable to create Label",
111111
getError(err, body),
112112
)
113113

114114
return
115115
}
116116

117117
outLabel := out.Data.Label
118-
plan.Key = types.StringValue(outLabel.Key)
119-
plan.Value = types.StringValue(outLabel.Value)
120-
plan.Description = types.StringPointerValue(outLabel.Description)
118+
var state models.LabelState
119+
state.Fill(api.LabelV1(outLabel))
121120

122121
// Set state to fully populated data
123122
diags = resp.State.Set(ctx, plan)
@@ -150,15 +149,27 @@ func (r *labelResource) Read(ctx context.Context, req resource.ReadRequest, resp
150149

151150
labels := response.Data.Labels
152151

153-
label := api.LabelV1{}
152+
var label *api.LabelV1
154153
for _, l := range labels {
155154
if l.Key == types.String.ValueString(state.Key) && l.Value == types.String.ValueString(state.Value) {
156-
label = l
155+
label = &api.LabelV1{
156+
Key: l.Key,
157+
Value: l.Value,
158+
Description: l.Description,
159+
}
157160
}
158161
}
159162

160-
state.Key = types.StringValue(label.Key)
161-
state.Value = types.StringValue(label.Value)
163+
if label == nil {
164+
resp.Diagnostics.AddError(
165+
"Unable to find Label",
166+
fmt.Sprintf("Unable to find Label with key: %q and value: %q", state.Key, state.Value),
167+
)
168+
169+
return
170+
}
171+
172+
state.Fill(*label)
162173
if label.Description != nil && *label.Description == "" {
163174
label.Description = nil
164175
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package models
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
5+
"github.com/hashicorp/terraform-plugin-framework/types"
6+
"github.com/segmentio/public-api-sdk-go/api"
7+
)
8+
9+
type ProfilesWarehouseState struct {
10+
ID types.String `tfsdk:"id"`
11+
SpaceID types.String `tfsdk:"space_id"`
12+
MetadataID types.String `tfsdk:"metadata_id"`
13+
Name types.String `tfsdk:"name"`
14+
Enabled types.Bool `tfsdk:"enabled"`
15+
SchemaName types.String `tfsdk:"schema_name"`
16+
Settings jsontypes.Normalized `tfsdk:"settings"`
17+
}
18+
19+
func (w *ProfilesWarehouseState) Fill(warehouse api.ProfilesWarehouse1) error {
20+
w.ID = types.StringValue(warehouse.Id)
21+
w.SpaceID = types.StringValue(warehouse.SpaceId)
22+
w.MetadataID = types.StringValue(warehouse.Metadata.Id)
23+
if warehouse.Settings.IsSet() {
24+
name := warehouse.Settings.Get().Get()["name"]
25+
if name != nil {
26+
stringName, ok := name.(string)
27+
if ok {
28+
w.Name = types.StringValue(stringName)
29+
}
30+
}
31+
}
32+
w.Enabled = types.BoolValue(warehouse.Enabled)
33+
w.SchemaName = types.StringPointerValue(warehouse.SchemaName)
34+
settings, err := GetSettings(warehouse.Settings)
35+
if err != nil {
36+
return err
37+
}
38+
w.Settings = settings
39+
40+
return nil
41+
}

internal/provider/models/source.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (s *SourceState) Fill(source api.Source4) error {
4545
if err != nil {
4646
return err
4747
}
48-
settings, err := s.getSettings(source.Settings)
48+
settings, err := GetSettings(source.Settings)
4949
if err != nil {
5050
return err
5151
}
@@ -77,7 +77,7 @@ func (s *SourceState) getWriteKeys(writeKeys []string) []types.String {
7777
return writeKeysToAdd
7878
}
7979

80-
func (s *SourceState) getSettings(settings api.NullableModelMap) (jsontypes.Normalized, error) {
80+
func GetSettings(settings api.NullableModelMap) (jsontypes.Normalized, error) {
8181
if !settings.IsSet() {
8282
return jsontypes.NewNormalizedNull(), nil
8383
}

0 commit comments

Comments
 (0)