|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/segmentio/terraform-provider-segment/internal/provider/models" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/segmentio/public-api-sdk-go/api" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + _ datasource.DataSource = &userDataSource{} |
| 16 | + _ datasource.DataSourceWithConfigure = &userDataSource{} |
| 17 | +) |
| 18 | + |
| 19 | +type userDataSource struct { |
| 20 | + client *api.APIClient |
| 21 | + authContext context.Context |
| 22 | +} |
| 23 | + |
| 24 | +func NewUserDataSource() datasource.DataSource { |
| 25 | + return &userDataSource{} |
| 26 | +} |
| 27 | + |
| 28 | +func (d *userDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 29 | + if req.ProviderData == nil { |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + config, ok := req.ProviderData.(*ClientInfo) |
| 34 | + if !ok { |
| 35 | + resp.Diagnostics.AddError( |
| 36 | + "Unexpected Data Source Configure Type", |
| 37 | + fmt.Sprintf("Expected ClientInfo, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 38 | + ) |
| 39 | + |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + d.client = config.client |
| 44 | + d.authContext = config.authContext |
| 45 | +} |
| 46 | + |
| 47 | +func (d *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 48 | + resp.TypeName = req.ProviderTypeName + "_user" |
| 49 | +} |
| 50 | + |
| 51 | +func (d *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 52 | + resp.Schema = schema.Schema{ |
| 53 | + Description: "A user belonging to a Segment Workspace.", |
| 54 | + Attributes: map[string]schema.Attribute{ |
| 55 | + "id": schema.StringAttribute{ |
| 56 | + Required: true, |
| 57 | + Description: "The unique identifier for this user.", |
| 58 | + }, |
| 59 | + "name": schema.StringAttribute{ |
| 60 | + Description: "The human-readable name of this user.", |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "email": schema.StringAttribute{ |
| 64 | + Description: "The email address associated with this user.", |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "permissions": schema.SetNestedAttribute{ |
| 68 | + Description: "The permissions associated with this user.", |
| 69 | + Computed: true, |
| 70 | + NestedObject: schema.NestedAttributeObject{ |
| 71 | + Attributes: map[string]schema.Attribute{ |
| 72 | + "role_id": schema.StringAttribute{ |
| 73 | + Description: "The id of the role associated with this permission.", |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "resources": schema.SetNestedAttribute{ |
| 77 | + Description: "The resources associated with this permission.", |
| 78 | + Computed: true, |
| 79 | + NestedObject: schema.NestedAttributeObject{ |
| 80 | + Attributes: map[string]schema.Attribute{ |
| 81 | + "id": schema.StringAttribute{ |
| 82 | + Description: "The id of this resource.", |
| 83 | + Computed: true, |
| 84 | + }, |
| 85 | + "type": schema.StringAttribute{ |
| 86 | + Description: "The type for this resource.", |
| 87 | + Computed: true, |
| 88 | + }, |
| 89 | + "labels": schema.SetNestedAttribute{ |
| 90 | + Description: "The labels that further refine access to this resource. Labels are exclusive to Workspace-level permissions.", |
| 91 | + Computed: true, |
| 92 | + NestedObject: schema.NestedAttributeObject{ |
| 93 | + Attributes: map[string]schema.Attribute{ |
| 94 | + "key": schema.StringAttribute{ |
| 95 | + Description: "The key that represents the name of this label.", |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + "value": schema.StringAttribute{ |
| 99 | + Description: "The value associated with the key of this label.", |
| 100 | + Computed: true, |
| 101 | + }, |
| 102 | + "description": schema.StringAttribute{ |
| 103 | + Description: "An optional description of the purpose of this label.", |
| 104 | + Computed: true, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 120 | + var config models.UserDataSourceState |
| 121 | + diags := req.Config.Get(ctx, &config) |
| 122 | + resp.Diagnostics.Append(diags...) |
| 123 | + if resp.Diagnostics.HasError() { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + id := config.ID.ValueString() |
| 128 | + if id == "" { |
| 129 | + resp.Diagnostics.AddError("Unable to read user", "ID is empty") |
| 130 | + |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + out, body, err := d.client.IAMUsersApi.GetUser(d.authContext, id).Execute() |
| 135 | + if body != nil { |
| 136 | + defer body.Body.Close() |
| 137 | + } |
| 138 | + if err != nil { |
| 139 | + resp.Diagnostics.AddError( |
| 140 | + "Unable to read user", |
| 141 | + getError(err, body), |
| 142 | + ) |
| 143 | + |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + var state models.UserDataSourceState |
| 148 | + err = state.Fill(api.UserV1(out.Data.User)) |
| 149 | + if err != nil { |
| 150 | + resp.Diagnostics.AddError( |
| 151 | + "Unable to read user", |
| 152 | + err.Error(), |
| 153 | + ) |
| 154 | + |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + diags = resp.State.Set(ctx, &state) |
| 159 | + resp.Diagnostics.Append(diags...) |
| 160 | + if resp.Diagnostics.HasError() { |
| 161 | + return |
| 162 | + } |
| 163 | +} |
0 commit comments