@@ -3,6 +3,7 @@ package account
3
3
import (
4
4
"context"
5
5
6
+ "github.com/google/uuid"
6
7
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
8
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8
9
accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3"
@@ -22,7 +23,7 @@ func DataSourceProject() *schema.Resource {
22
23
Type : schema .TypeString ,
23
24
Computed : true ,
24
25
Optional : true ,
25
- Description : "The ID of the SSH key " ,
26
+ Description : "The ID of the project " ,
26
27
ValidateDiagFunc : verify .IsUUID (),
27
28
}
28
29
@@ -85,3 +86,105 @@ func DataSourceAccountProjectRead(ctx context.Context, d *schema.ResourceData, m
85
86
86
87
return nil
87
88
}
89
+
90
+ func DataSourceProjects () * schema.Resource {
91
+ dsSchema := datasource .SchemaFromResourceSchema (ResourceProject ().Schema )
92
+ datasource .AddOptionalFieldsToSchema (dsSchema , "organization_id" )
93
+
94
+ dsSchema ["organization_id" ] = & schema.Schema {
95
+ Type : schema .TypeString ,
96
+ Computed : true ,
97
+ Optional : true ,
98
+ Description : "The ID of the organization" ,
99
+ ValidateDiagFunc : verify .IsUUID (),
100
+ }
101
+ dsSchema ["projects" ] = & schema.Schema {
102
+ Type : schema .TypeList ,
103
+ Computed : true ,
104
+ Description : "The list of projects" ,
105
+ Elem : & schema.Resource {
106
+ Schema : map [string ]* schema.Schema {
107
+ "id" : {
108
+ Type : schema .TypeString ,
109
+ Computed : true ,
110
+ Description : "ID of the Project" ,
111
+ },
112
+ "name" : {
113
+ Type : schema .TypeString ,
114
+ Computed : true ,
115
+ Description : "Name of the Project" ,
116
+ },
117
+ "organization_id" : {
118
+ Type : schema .TypeString ,
119
+ Computed : true ,
120
+ Description : "Organization ID of the Project" ,
121
+ },
122
+ "created_at" : {
123
+ Type : schema .TypeString ,
124
+ Computed : true ,
125
+ Description : "Creation date of the Project" ,
126
+ },
127
+ "updated_at" : {
128
+ Type : schema .TypeString ,
129
+ Computed : true ,
130
+ Description : "Update date of the Project" ,
131
+ },
132
+ "description" : {
133
+ Type : schema .TypeString ,
134
+ Computed : true ,
135
+ Description : "Description of the Project" ,
136
+ },
137
+ },
138
+ },
139
+ }
140
+
141
+ return & schema.Resource {
142
+ ReadContext : DataSourceAccountProjectsRead ,
143
+ Schema : dsSchema ,
144
+ }
145
+ }
146
+
147
+ func DataSourceAccountProjectsRead (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
148
+ accountAPI := NewProjectAPI (m )
149
+
150
+ var orgID * string
151
+
152
+ if v , orgIDExists := d .GetOk ("organization_id" ); orgIDExists {
153
+ orgID = types .ExpandStringPtr (v )
154
+ } else {
155
+ orgID = GetOrganizationID (m , d )
156
+ }
157
+
158
+ if orgID == nil {
159
+ return diag .Errorf ("organization_id was not specified nor found in the provider configuration" )
160
+ }
161
+
162
+ res , err := accountAPI .ListProjects (& accountSDK.ProjectAPIListProjectsRequest {
163
+ OrganizationID : * orgID ,
164
+ }, scw .WithContext (ctx ))
165
+ if err != nil {
166
+ return diag .FromErr (err )
167
+ }
168
+
169
+ d .SetId (uuid .New ().String ())
170
+ _ = d .Set ("projects" , flattenProjects (res .Projects ))
171
+ _ = d .Set ("organization_id" , orgID )
172
+
173
+ return nil
174
+ }
175
+
176
+ func flattenProjects (projects []* accountSDK.Project ) []map [string ]interface {} {
177
+ flattenedProjects := make ([]map [string ]interface {}, len (projects ))
178
+ for i , project := range projects {
179
+ flattenedProjects [i ] = map [string ]interface {}{
180
+ "id" : project .ID ,
181
+ "name" : project .Name ,
182
+ "organization_id" : project .OrganizationID ,
183
+ "created_at" : types .FlattenTime (project .CreatedAt ),
184
+ "updated_at" : types .FlattenTime (project .UpdatedAt ),
185
+ "description" : project .Description ,
186
+ }
187
+ }
188
+
189
+ return flattenedProjects
190
+ }
0 commit comments