|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/temporalio/terraform-provider-temporalcloud/internal/client" |
| 13 | + "github.com/temporalio/terraform-provider-temporalcloud/internal/provider/enums" |
| 14 | + "go.temporal.io/cloud-sdk/api/cloudservice/v1" |
| 15 | + connectivityrulev1 "go.temporal.io/cloud-sdk/api/connectivityrule/v1" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + _ datasource.DataSource = &connectivityRuleDataSource{} |
| 20 | + _ datasource.DataSourceWithConfigure = &connectivityRuleDataSource{} |
| 21 | +) |
| 22 | + |
| 23 | +func NewConnectivityRuleDataSource() datasource.DataSource { |
| 24 | + return &connectivityRuleDataSource{} |
| 25 | +} |
| 26 | + |
| 27 | +type ( |
| 28 | + connectivityRuleDataSource struct { |
| 29 | + client *client.Client |
| 30 | + } |
| 31 | + |
| 32 | + connectivityRuleDataModel struct { |
| 33 | + ID types.String `tfsdk:"id"` |
| 34 | + ConnectivityType types.String `tfsdk:"connectivity_type"` |
| 35 | + ConnectionID types.String `tfsdk:"connection_id"` |
| 36 | + Region types.String `tfsdk:"region"` |
| 37 | + GcpProjectID types.String `tfsdk:"gcp_project_id"` |
| 38 | + |
| 39 | + State types.String `tfsdk:"state"` |
| 40 | + CreatedAt types.String `tfsdk:"created_at"` |
| 41 | + } |
| 42 | +) |
| 43 | + |
| 44 | +func connectivityRuleDataSourceSchema(idRequired bool) map[string]schema.Attribute { |
| 45 | + idAttribute := schema.StringAttribute{ |
| 46 | + Description: "The unique identifier of the connectivity rule across all Temporal Cloud tenants.", |
| 47 | + } |
| 48 | + |
| 49 | + switch idRequired { |
| 50 | + case true: |
| 51 | + idAttribute.Required = true |
| 52 | + case false: |
| 53 | + idAttribute.Computed = true |
| 54 | + } |
| 55 | + |
| 56 | + return map[string]schema.Attribute{ |
| 57 | + "id": idAttribute, |
| 58 | + "connectivity_type": schema.StringAttribute{ |
| 59 | + Computed: true, |
| 60 | + Description: "The type of connectivity.", |
| 61 | + }, |
| 62 | + "connection_id": schema.StringAttribute{ |
| 63 | + Computed: true, |
| 64 | + Description: "The ID of the connection to the connectivity rule.", |
| 65 | + }, |
| 66 | + "region": schema.StringAttribute{ |
| 67 | + Computed: true, |
| 68 | + Description: "The region of the connectivity rule.", |
| 69 | + }, |
| 70 | + "gcp_project_id": schema.StringAttribute{ |
| 71 | + Computed: true, |
| 72 | + Description: "The GCP project ID of the connectivity rule.", |
| 73 | + }, |
| 74 | + "state": schema.StringAttribute{ |
| 75 | + Computed: true, |
| 76 | + Description: "The current state of the connectivity rule.", |
| 77 | + }, |
| 78 | + "created_at": schema.StringAttribute{ |
| 79 | + Computed: true, |
| 80 | + Description: "The time the connectivity rule was created.", |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func (d *connectivityRuleDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 86 | + if req.ProviderData == nil { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + client, ok := req.ProviderData.(*client.Client) |
| 91 | + if !ok { |
| 92 | + resp.Diagnostics.AddError( |
| 93 | + "Unexpected Data Source Configure Type", |
| 94 | + fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 95 | + ) |
| 96 | + |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + d.client = client |
| 101 | +} |
| 102 | + |
| 103 | +func (d *connectivityRuleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 104 | + resp.TypeName = req.ProviderTypeName + "_connectivity_rule" |
| 105 | +} |
| 106 | + |
| 107 | +func (d *connectivityRuleDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 108 | + resp.Schema = schema.Schema{ |
| 109 | + Description: "Fetches details about a connectivity rule.", |
| 110 | + Attributes: connectivityRuleDataSourceSchema(true), |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (d *connectivityRuleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 115 | + var input connectivityRuleDataModel |
| 116 | + resp.Diagnostics.Append(req.Config.Get(ctx, &input)...) |
| 117 | + if resp.Diagnostics.HasError() { |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + if len(input.ID.ValueString()) == 0 { |
| 122 | + resp.Diagnostics.AddError("invalid connectivity rule id", "connectivity rule id is required") |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + crResp, err := d.client.CloudService().GetConnectivityRule(ctx, &cloudservice.GetConnectivityRuleRequest{ |
| 127 | + ConnectivityRuleId: input.ID.ValueString(), |
| 128 | + }) |
| 129 | + if err != nil { |
| 130 | + resp.Diagnostics.AddError("Failed to get connectivity rule", err.Error()) |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + model, diags := connectivityRuleToConnectivityRuleDataModel(crResp.GetConnectivityRule()) |
| 135 | + resp.Diagnostics.Append(diags...) |
| 136 | + if resp.Diagnostics.HasError() { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + diags = resp.State.Set(ctx, model) |
| 141 | + resp.Diagnostics.Append(diags...) |
| 142 | +} |
| 143 | + |
| 144 | +func connectivityRuleToConnectivityRuleDataModel(connectivityRule *connectivityrulev1.ConnectivityRule) (*connectivityRuleDataModel, diag.Diagnostics) { |
| 145 | + var diags diag.Diagnostics |
| 146 | + |
| 147 | + stateStr, err := enums.FromResourceState(connectivityRule.State) |
| 148 | + if err != nil { |
| 149 | + diags.AddError("Unable to convert connectivity rule state", err.Error()) |
| 150 | + return nil, diags |
| 151 | + } |
| 152 | + |
| 153 | + model := new(connectivityRuleDataModel) |
| 154 | + model.ID = types.StringValue(connectivityRule.GetId()) |
| 155 | + model.State = types.StringValue(stateStr) |
| 156 | + model.CreatedAt = types.StringValue(connectivityRule.GetCreatedTime().AsTime().Format(time.RFC3339)) |
| 157 | + |
| 158 | + if connectivityRule.GetSpec().GetPrivateRule() != nil { |
| 159 | + model.ConnectivityType = types.StringValue(connectivityRuleTypePrivate) |
| 160 | + model.ConnectionID = types.StringValue(connectivityRule.GetSpec().GetPrivateRule().GetConnectionId()) |
| 161 | + model.Region = types.StringValue(connectivityRule.GetSpec().GetPrivateRule().GetRegion()) |
| 162 | + if connectivityRule.GetSpec().GetPrivateRule().GetGcpProjectId() != "" { |
| 163 | + model.GcpProjectID = types.StringValue(connectivityRule.GetSpec().GetPrivateRule().GetGcpProjectId()) |
| 164 | + } else { |
| 165 | + model.GcpProjectID = types.StringNull() |
| 166 | + } |
| 167 | + } else if connectivityRule.GetSpec().GetPublicRule() != nil { |
| 168 | + model.ConnectivityType = types.StringValue(connectivityRuleTypePublic) |
| 169 | + model.ConnectionID = types.StringNull() |
| 170 | + model.Region = types.StringNull() |
| 171 | + model.GcpProjectID = types.StringNull() |
| 172 | + } else { |
| 173 | + diags.AddError("Invalid connectivity rule", "connectivity rule must be either public or private") |
| 174 | + return nil, diags |
| 175 | + } |
| 176 | + return model, diags |
| 177 | +} |
0 commit comments