|
| 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 | +} |
0 commit comments