|
| 1 | +package group_users |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | +) |
| 11 | + |
| 12 | +// Ensure the implementation satisfies the expected interfaces. |
| 13 | +var ( |
| 14 | + _ datasource.DataSource = &groupUsersDataSource{} |
| 15 | + _ datasource.DataSourceWithConfigure = &groupUsersDataSource{} |
| 16 | +) |
| 17 | + |
| 18 | +// GroupUsersDataSource is a helper function to simplify the provider implementation. |
| 19 | +func GroupUsersDataSource() datasource.DataSource { |
| 20 | + return &groupUsersDataSource{} |
| 21 | +} |
| 22 | + |
| 23 | +// groupUsersDataSource is the data source implementation. |
| 24 | +type groupUsersDataSource struct { |
| 25 | + client *dbt_cloud.Client |
| 26 | +} |
| 27 | + |
| 28 | +// Configure adds the provider configured client to the data source. |
| 29 | +func (d *groupUsersDataSource) Configure( |
| 30 | + ctx context.Context, |
| 31 | + req datasource.ConfigureRequest, |
| 32 | + resp *datasource.ConfigureResponse, |
| 33 | +) { |
| 34 | + if req.ProviderData == nil { |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + client, ok := req.ProviderData.(*dbt_cloud.Client) |
| 39 | + if !ok { |
| 40 | + resp.Diagnostics.AddError( |
| 41 | + "Unexpected Data Source Configure Type", |
| 42 | + fmt.Sprintf( |
| 43 | + "Expected *dbt_cloud.Client, got: %T. Please report this issue to the provider developers.", |
| 44 | + req.ProviderData, |
| 45 | + ), |
| 46 | + ) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + d.client = client |
| 51 | +} |
| 52 | + |
| 53 | +// Metadata returns the data source type name. |
| 54 | +func (d *groupUsersDataSource) Metadata( |
| 55 | + ctx context.Context, |
| 56 | + req datasource.MetadataRequest, |
| 57 | + resp *datasource.MetadataResponse, |
| 58 | +) { |
| 59 | + resp.TypeName = req.ProviderTypeName + "_group_users" |
| 60 | +} |
| 61 | + |
| 62 | +// Schema defines the schema for the data source. |
| 63 | +func (d *groupUsersDataSource) Schema( |
| 64 | + ctx context.Context, |
| 65 | + req datasource.SchemaRequest, |
| 66 | + resp *datasource.SchemaResponse, |
| 67 | +) { |
| 68 | + resp.Schema = dataSourceSchema |
| 69 | +} |
| 70 | + |
| 71 | +// Read refreshes the Terraform state with the latest data. |
| 72 | +func (d *groupUsersDataSource) Read( |
| 73 | + ctx context.Context, |
| 74 | + req datasource.ReadRequest, |
| 75 | + resp *datasource.ReadResponse, |
| 76 | +) { |
| 77 | + var state GroupUsersDataSourceModel |
| 78 | + diags := req.Config.Get(ctx, &state) |
| 79 | + resp.Diagnostics.Append(diags...) |
| 80 | + if resp.Diagnostics.HasError() { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + groupID := int(state.GroupID.ValueInt64()) |
| 85 | + |
| 86 | + users, err := d.client.GetUsers() |
| 87 | + if err != nil { |
| 88 | + resp.Diagnostics.AddError( |
| 89 | + "Error reading users", |
| 90 | + "Could not read users "+state.ID.ValueString()+": "+err.Error(), |
| 91 | + ) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + usersModels := []userDataSourceModel{} |
| 96 | + for _, user := range users { |
| 97 | + userGroups := user.Permissions[0].Groups |
| 98 | + |
| 99 | + userInGroup := false |
| 100 | + for _, userGroup := range userGroups { |
| 101 | + if userGroup.ID == groupID { |
| 102 | + userInGroup = true |
| 103 | + // we can stop looping |
| 104 | + break |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if userInGroup { |
| 109 | + userModel := userDataSourceModel{ |
| 110 | + ID: types.Int64Value(int64(user.ID)), |
| 111 | + Email: types.StringValue(user.Email), |
| 112 | + } |
| 113 | + usersModels = append(usersModels, userModel) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + state.Users = usersModels |
| 118 | + |
| 119 | + // Set state |
| 120 | + diags = resp.State.Set(ctx, &state) |
| 121 | + resp.Diagnostics.Append(diags...) |
| 122 | + if resp.Diagnostics.HasError() { |
| 123 | + return |
| 124 | + } |
| 125 | +} |
0 commit comments