Skip to content

Commit f56f6c6

Browse files
committed
feat(provider): introduce braze destination resource
1 parent 282f3e0 commit f56f6c6

22 files changed

+899
-3
lines changed

internal/provider/big_query_destination_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (r *bigQueryDestinationResource) MoveState(ctx context.Context) []resource.
5858
{
5959
SourceSchema: &schema,
6060
StateMover: func(_ context.Context, req resource.MoveStateRequest, resp *resource.MoveStateResponse) {
61-
if req.SourceTypeName == "censusworkspace_destination" && req.SourceSchemaVersion == 0 {
61+
if req.SourceTypeName == DestinationResourceTypeName && req.SourceSchemaVersion == 0 {
6262
destinationModel := DestinationModel{}
6363
resp.Diagnostics.Append(req.SourceState.Get(ctx, &destinationModel)...)
6464

internal/provider/big_query_destination_resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestAccBigQueryDestinationResourceImportNotFound(t *testing.T) {
6565
})
6666
}
6767

68-
//nolint:paralleltest
68+
//nolint:dupl,paralleltest
6969
func TestAccBigQueryDestinationResourceCreateUpdateDelete(t *testing.T) {
7070
server, err := cmt.NewCensusManagementServer()
7171
require.NoError(t, err)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/go-faster/jx"
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/hashicorp/terraform-plugin-framework/path"
9+
)
10+
11+
func NewBrazeDestinationConnectionDetailsFromResponse(_ context.Context, path path.Path, data jx.Raw) (TypedObject[BrazeDestinationConnectionDetails], diag.Diagnostics) {
12+
if data == nil {
13+
return NewTypedObjectNull[BrazeDestinationConnectionDetails](), nil
14+
}
15+
16+
diags := diag.Diagnostics{}
17+
18+
var connectionDetails BrazeDestinationConnectionDetails
19+
20+
dec := jx.DecodeBytes(data)
21+
22+
err := connectionDetails.Decode(dec)
23+
if err != nil {
24+
diags.AddAttributeError(path, "Error decoding value", err.Error())
25+
}
26+
27+
return NewTypedObject(connectionDetails), diags
28+
}
29+
30+
func (cd *BrazeDestinationConnectionDetails) Decode(dec *jx.Decoder) error {
31+
//nolint:wrapcheck
32+
return dec.Obj(func(dec *jx.Decoder, key string) error {
33+
switch key {
34+
case "instance_url":
35+
return JxDecodeStringValue(dec, &cd.InstanceURL)
36+
37+
default:
38+
return dec.Skip()
39+
}
40+
})
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package provider_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/cysp/terraform-provider-censusworkspace/internal/provider"
7+
"github.com/hashicorp/terraform-plugin-framework/path"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestNewBrazeDestinationConnectionDetailsFromResponse(t *testing.T) {
13+
t.Parallel()
14+
15+
testcases := map[string]struct {
16+
input []byte
17+
expected TypedObject[BrazeDestinationConnectionDetails]
18+
}{
19+
"null": {
20+
input: nil,
21+
expected: NewTypedObjectNull[BrazeDestinationConnectionDetails](),
22+
},
23+
"empty": {
24+
input: []byte(`{}`),
25+
expected: NewTypedObject(BrazeDestinationConnectionDetails{}),
26+
},
27+
"instance_url": {
28+
input: []byte(`{"instance_url":"instance-url"}`),
29+
expected: NewTypedObject(BrazeDestinationConnectionDetails{
30+
InstanceURL: types.StringValue("instance-url"),
31+
}),
32+
},
33+
"instance_url=null": {
34+
input: []byte(`{"instance_url":null}`),
35+
expected: NewTypedObject(BrazeDestinationConnectionDetails{
36+
InstanceURL: types.StringNull(),
37+
}),
38+
},
39+
}
40+
41+
for name, testcase := range testcases {
42+
t.Run(name, func(t *testing.T) {
43+
t.Parallel()
44+
45+
actual, diags := NewBrazeDestinationConnectionDetailsFromResponse(t.Context(), path.Root("connection_details"), testcase.input)
46+
assert.Empty(t, diags)
47+
48+
assert.Equal(t, testcase.expected, actual)
49+
})
50+
}
51+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package provider
2+
3+
func (c *BrazeDestinationCredentials) UpdateWithConnectionDetails(connectionDetails BrazeDestinationConnectionDetails) {
4+
c.InstanceURL = connectionDetails.InstanceURL
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package provider
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
)
6+
7+
const (
8+
BrazeDestinationType = "braze"
9+
)
10+
11+
type BrazeDestinationModel struct {
12+
destinationModelBase
13+
14+
Credentials TypedObject[BrazeDestinationCredentials] `tfsdk:"credentials"`
15+
ConnectionDetails TypedObject[BrazeDestinationConnectionDetails] `tfsdk:"connection_details"`
16+
}
17+
18+
//nolint:recvcheck
19+
type BrazeDestinationCredentials struct {
20+
InstanceURL types.String `tfsdk:"instance_url"`
21+
APIKey types.String `tfsdk:"api_key"`
22+
ClientKey types.String `tfsdk:"client_key"`
23+
}
24+
25+
type BrazeDestinationConnectionDetails struct {
26+
InstanceURL types.String `tfsdk:"instance_url"`
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
cm "github.com/cysp/terraform-provider-censusworkspace/internal/census-management-go"
7+
"github.com/go-faster/jx"
8+
"github.com/hashicorp/terraform-plugin-framework/diag"
9+
"github.com/hashicorp/terraform-plugin-framework/path"
10+
)
11+
12+
func (m *BrazeDestinationModel) ToCreateDestinationData(_ context.Context) (cm.CreateDestinationBody, diag.Diagnostics) {
13+
diags := diag.Diagnostics{}
14+
15+
body := cm.CreateDestinationBody{
16+
ServiceConnection: cm.CreateDestinationBodyServiceConnection{
17+
Name: m.Name.ValueString(),
18+
Type: BrazeDestinationType,
19+
},
20+
}
21+
22+
enc := jx.Encoder{}
23+
if !m.Credentials.IsNull() && !m.Credentials.IsUnknown() {
24+
credentialsEncodeFailed := m.Credentials.Value().Encode(&enc)
25+
if credentialsEncodeFailed {
26+
diags.AddAttributeError(path.Root("credentials"), "Failed to encode value", "")
27+
}
28+
29+
body.ServiceConnection.Credentials = enc.Bytes()
30+
}
31+
32+
return body, diags
33+
}
34+
35+
func (m *BrazeDestinationModel) ToUpdateDestinationData(_ context.Context) (cm.UpdateDestinationBody, diag.Diagnostics) {
36+
diags := diag.Diagnostics{}
37+
38+
body := cm.UpdateDestinationBody{
39+
ServiceConnection: cm.UpdateDestinationBodyServiceConnection{
40+
Name: cm.NewOptString(m.Name.ValueString()),
41+
},
42+
}
43+
44+
enc := jx.Encoder{}
45+
if !m.Credentials.IsNull() && !m.Credentials.IsUnknown() {
46+
credentialsEncodeFailed := m.Credentials.Value().Encode(&enc)
47+
if credentialsEncodeFailed {
48+
diags.AddAttributeError(path.Root("credentials"), "Failed to encode value", "")
49+
}
50+
51+
body.ServiceConnection.Credentials = enc.Bytes()
52+
}
53+
54+
return body, diags
55+
}
56+
57+
func (c BrazeDestinationCredentials) Encode(enc *jx.Encoder) bool {
58+
return enc.Obj(func(enc *jx.Encoder) {
59+
enc.Field("instance_url", func(e *jx.Encoder) {
60+
e.Str(c.InstanceURL.ValueString())
61+
})
62+
63+
enc.Field("api_key", func(e *jx.Encoder) {
64+
e.Str(c.APIKey.ValueString())
65+
})
66+
67+
clientKey := c.ClientKey.ValueString()
68+
if clientKey != "" {
69+
enc.Field("client_key", func(e *jx.Encoder) {
70+
e.Str(clientKey)
71+
})
72+
}
73+
})
74+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//nolint:dupl
2+
package provider
3+
4+
import (
5+
"context"
6+
"strconv"
7+
8+
cm "github.com/cysp/terraform-provider-censusworkspace/internal/census-management-go"
9+
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/path"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
)
14+
15+
func NewBrazeDestinationModelFromResponse(ctx context.Context, response cm.DestinationData) (BrazeDestinationModel, diag.Diagnostics) {
16+
diags := diag.Diagnostics{}
17+
18+
path := path.Empty()
19+
20+
model := BrazeDestinationModel{
21+
destinationModelBase: destinationModelBase{
22+
ID: types.StringValue(strconv.FormatInt(response.ID, 10)),
23+
Name: types.StringValue(response.Name),
24+
CreatedAt: timetypes.NewRFC3339TimeValue(response.CreatedAt),
25+
},
26+
}
27+
28+
if response.ConnectionDetails != nil {
29+
path := path.AtName("connection_details")
30+
31+
connectionDetails, connectionDetailsDiags := NewBrazeDestinationConnectionDetailsFromResponse(ctx, path, response.ConnectionDetails)
32+
diags.Append(connectionDetailsDiags...)
33+
34+
model.ConnectionDetails = connectionDetails
35+
}
36+
37+
if lastTestedAt, lastTestedAtOk := response.LastTestedAt.Get(); lastTestedAtOk {
38+
model.LastTestedAt = timetypes.NewRFC3339TimeValue(lastTestedAt)
39+
}
40+
41+
if lastTestSucceeded, lastTestSucceededOk := response.LastTestSucceeded.Get(); lastTestSucceededOk {
42+
model.LastTestSucceeded = types.BoolValue(lastTestSucceeded)
43+
}
44+
45+
return model, diags
46+
}

0 commit comments

Comments
 (0)