|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"fmt" |
| 6 | + |
| 7 | +"github.com/coder/coder/v2/codersdk" |
| 8 | +"github.com/google/uuid" |
| 9 | +"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" |
| 10 | +"github.com/hashicorp/terraform-plugin-framework/attr" |
| 11 | +"github.com/hashicorp/terraform-plugin-framework/datasource" |
| 12 | +"github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 13 | +"github.com/hashicorp/terraform-plugin-framework/path" |
| 14 | +"github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | +) |
| 16 | + |
| 17 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 18 | +var_ datasource.DataSource=&OrganizationDataSource{} |
| 19 | +var_ datasource.DataSourceWithConfigValidators=&OrganizationDataSource{} |
| 20 | + |
| 21 | +funcNewOrganizationDataSource() datasource.DataSource { |
| 22 | +return&OrganizationDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +// OrganizationDataSource defines the data source implementation. |
| 26 | +typeOrganizationDataSourcestruct { |
| 27 | +data*CoderdProviderData |
| 28 | +} |
| 29 | + |
| 30 | +// OrganizationDataSourceModel describes the data source data model. |
| 31 | +typeOrganizationDataSourceModelstruct { |
| 32 | +// Exactly one of ID, IsDefault, or Name must be set. |
| 33 | +ID types.String`tfsdk:"id"` |
| 34 | +IsDefault types.Bool`tfsdk:"is_default"` |
| 35 | +Name types.String`tfsdk:"name"` |
| 36 | + |
| 37 | +CreatedAt types.Int64`tfsdk:"created_at"` |
| 38 | +UpdatedAt types.Int64`tfsdk:"updated_at"` |
| 39 | +// TODO: This could reasonably store some User object - though we may need to make additional queries depending on what fields we |
| 40 | +// want, or to have one consistent user type for all data sources. |
| 41 | +Members types.Set`tfsdk:"members"` |
| 42 | +} |
| 43 | + |
| 44 | +func (d*OrganizationDataSource)Metadata(ctx context.Context,req datasource.MetadataRequest,resp*datasource.MetadataResponse) { |
| 45 | +resp.TypeName=req.ProviderTypeName+"_organization" |
| 46 | +} |
| 47 | + |
| 48 | +func (d*OrganizationDataSource)Schema(ctx context.Context,req datasource.SchemaRequest,resp*datasource.SchemaResponse) { |
| 49 | +resp.Schema= schema.Schema{ |
| 50 | +MarkdownDescription:"An existing organization on the coder deployment.", |
| 51 | + |
| 52 | +Attributes:map[string]schema.Attribute{ |
| 53 | +"id": schema.StringAttribute{ |
| 54 | +MarkdownDescription:"The ID of the organization to retrieve. This field will be populated if the organization is found by name, or if the default organization is requested.", |
| 55 | +Optional:true, |
| 56 | +Computed:true, |
| 57 | +}, |
| 58 | +"is_default": schema.BoolAttribute{ |
| 59 | +MarkdownDescription:"Whether the organization is the default organization of the deployment. This field will be populated if the organization is found by ID or name.", |
| 60 | +Optional:true, |
| 61 | +Computed:true, |
| 62 | +}, |
| 63 | +"name": schema.StringAttribute{ |
| 64 | +MarkdownDescription:"The name of the organization to retrieve. This field will be populated if the organization is found by ID, or if the default organization is requested.", |
| 65 | +Optional:true, |
| 66 | +Computed:true, |
| 67 | +}, |
| 68 | +"created_at": schema.Int64Attribute{ |
| 69 | +MarkdownDescription:"Unix timestamp when the organization was created.", |
| 70 | +Computed:true, |
| 71 | +}, |
| 72 | +"updated_at": schema.Int64Attribute{ |
| 73 | +MarkdownDescription:"Unix timestamp when the organization was last updated.", |
| 74 | +Computed:true, |
| 75 | +}, |
| 76 | + |
| 77 | +"members": schema.SetAttribute{ |
| 78 | +MarkdownDescription:"Members of the organization, by ID", |
| 79 | +Computed:true, |
| 80 | +ElementType:types.StringType, |
| 81 | +}, |
| 82 | +}, |
| 83 | +} |
| 84 | +} |
| 85 | + |
| 86 | +func (d*OrganizationDataSource)Configure(ctx context.Context,req datasource.ConfigureRequest,resp*datasource.ConfigureResponse) { |
| 87 | +// Prevent panic if the provider has not been configured. |
| 88 | +ifreq.ProviderData==nil { |
| 89 | +return |
| 90 | +} |
| 91 | + |
| 92 | +data,ok:=req.ProviderData.(*CoderdProviderData) |
| 93 | + |
| 94 | +if!ok { |
| 95 | +resp.Diagnostics.AddError( |
| 96 | +"Unexpected Data Source Configure Type", |
| 97 | +fmt.Sprintf("Expected *CoderdProviderData, got: %T. Please report this issue to the provider developers.",req.ProviderData), |
| 98 | +) |
| 99 | + |
| 100 | +return |
| 101 | +} |
| 102 | + |
| 103 | +d.data=data |
| 104 | +} |
| 105 | + |
| 106 | +func (d*OrganizationDataSource)Read(ctx context.Context,req datasource.ReadRequest,resp*datasource.ReadResponse) { |
| 107 | +vardataOrganizationDataSourceModel |
| 108 | + |
| 109 | +// Read Terraform configuration data into the model |
| 110 | +resp.Diagnostics.Append(req.Config.Get(ctx,&data)...) |
| 111 | + |
| 112 | +ifresp.Diagnostics.HasError() { |
| 113 | +return |
| 114 | +} |
| 115 | + |
| 116 | +client:=d.data.Client |
| 117 | + |
| 118 | +varorg codersdk.Organization |
| 119 | +if!data.ID.IsNull() {// By ID |
| 120 | +orgID,err:=uuid.Parse(data.ID.ValueString()) |
| 121 | +iferr!=nil { |
| 122 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Unable to parse supplied ID as UUID, got error: %s",err)) |
| 123 | +return |
| 124 | +} |
| 125 | +org,err=client.Organization(ctx,orgID) |
| 126 | +iferr!=nil { |
| 127 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Unable to get organization by ID, got error: %s",err)) |
| 128 | +return |
| 129 | +} |
| 130 | +iforg.ID.String()!=data.ID.ValueString() { |
| 131 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Organization ID %s does not match requested ID %s",org.ID,data.ID)) |
| 132 | +return |
| 133 | +} |
| 134 | +}elseifdata.IsDefault.ValueBool() {// Get Default |
| 135 | +varerrerror |
| 136 | +org,err=client.OrganizationByName(ctx,"default") |
| 137 | +iferr!=nil { |
| 138 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Unable to get default organization, got error: %s",err)) |
| 139 | +return |
| 140 | +} |
| 141 | +if!org.IsDefault { |
| 142 | +resp.Diagnostics.AddError("Client Error","Found organization was not the default organization") |
| 143 | +return |
| 144 | +} |
| 145 | +}else {// By Name |
| 146 | +varerrerror |
| 147 | +org,err=client.OrganizationByName(ctx,data.Name.ValueString()) |
| 148 | +iferr!=nil { |
| 149 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Unable to get organization by name, got error: %s",err)) |
| 150 | +return |
| 151 | +} |
| 152 | +iforg.Name!=data.Name.ValueString() { |
| 153 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Organization name %s does not match requested name %s",org.Name,data.Name)) |
| 154 | +return |
| 155 | +} |
| 156 | +} |
| 157 | +data.ID=types.StringValue(org.ID.String()) |
| 158 | +data.Name=types.StringValue(org.Name) |
| 159 | +data.IsDefault=types.BoolValue(org.IsDefault) |
| 160 | +data.CreatedAt=types.Int64Value(org.CreatedAt.Unix()) |
| 161 | +data.UpdatedAt=types.Int64Value(org.UpdatedAt.Unix()) |
| 162 | +members,err:=client.OrganizationMembers(ctx,org.ID) |
| 163 | +iferr!=nil { |
| 164 | +resp.Diagnostics.AddError("Client Error",fmt.Sprintf("Unable to get organization members, got error: %s",err)) |
| 165 | +return |
| 166 | +} |
| 167 | +memberIDs:=make([]attr.Value,0,len(members)) |
| 168 | +for_,member:=rangemembers { |
| 169 | +memberIDs=append(memberIDs,types.StringValue(member.UserID.String())) |
| 170 | +} |
| 171 | +data.Members=types.SetValueMust(types.StringType,memberIDs) |
| 172 | + |
| 173 | +// Save data into Terraform state |
| 174 | +resp.Diagnostics.Append(resp.State.Set(ctx,&data)...) |
| 175 | +} |
| 176 | + |
| 177 | +func (d*OrganizationDataSource)ConfigValidators(_ context.Context) []datasource.ConfigValidator { |
| 178 | +return []datasource.ConfigValidator{ |
| 179 | +datasourcevalidator.ExactlyOneOf( |
| 180 | +path.MatchRoot("id"), |
| 181 | +path.MatchRoot("is_default"), |
| 182 | +path.MatchRoot("name"), |
| 183 | +), |
| 184 | +} |
| 185 | +} |