Skip to content

Commit 59d63aa

Browse files
authored
feat(account): extract project id if not given (#1886)
1 parent d8d5a98 commit 59d63aa

File tree

3 files changed

+206
-5
lines changed

3 files changed

+206
-5
lines changed

scaleway/data_source_account_project.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func dataSourceScalewayAccountProject() *schema.Resource {
1616
dsSchema["name"].ConflictsWith = []string{"project_id"}
1717
dsSchema["project_id"] = &schema.Schema{
1818
Type: schema.TypeString,
19+
Computed: true,
1920
Optional: true,
2021
Description: "The ID of the SSH key",
2122
ValidateFunc: validationUUID(),
@@ -30,22 +31,23 @@ func dataSourceScalewayAccountProject() *schema.Resource {
3031
func dataSourceScalewayAccountProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
3132
accountAPI := accountV2API(meta)
3233

33-
projectID, projectIDExists := d.GetOk("project_id")
34-
if !projectIDExists {
34+
var projectID string
35+
36+
if name, nameExists := d.GetOk("name"); nameExists {
3537
orgID := getOrganizationID(meta, d)
3638
if orgID == nil {
3739
// required not in schema as we could use default
3840
return diag.Errorf("organization_id is required with name")
3941
}
4042
res, err := accountAPI.ListProjects(&accountV2.ListProjectsRequest{
4143
OrganizationID: *orgID,
42-
Name: expandStringPtr(d.Get("name")),
44+
Name: expandStringPtr(name),
4345
}, scw.WithContext(ctx))
4446
if err != nil {
4547
return diag.FromErr(err)
4648
}
4749
for _, project := range res.Projects {
48-
if project.Name == d.Get("name").(string) {
50+
if project.Name == name {
4951
if projectID != "" {
5052
return diag.Errorf("more than 1 project found with the same name %s", d.Get("name"))
5153
}
@@ -55,9 +57,16 @@ func dataSourceScalewayAccountProjectRead(ctx context.Context, d *schema.Resourc
5557
if projectID == "" {
5658
diag.Errorf("no project found with the name %s", d.Get("name"))
5759
}
60+
} else {
61+
extractedProjectID, _, err := extractProjectID(d, meta.(*Meta))
62+
if err != nil {
63+
return diag.FromErr(err)
64+
}
65+
66+
projectID = extractedProjectID
5867
}
5968

60-
d.SetId(projectID.(string))
69+
d.SetId(projectID)
6170
_ = d.Set("project_id", projectID)
6271

6372
diags := resourceScalewayAccountProjectRead(ctx, d, meta)

scaleway/data_source_account_project_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,27 @@ func TestAccScalewayDataSourceAccountProject_Default(t *testing.T) {
8787
},
8888
})
8989
}
90+
91+
func TestAccScalewayDataSourceAccountProject_Extract(t *testing.T) {
92+
tt := NewTestTools(t)
93+
defer tt.Cleanup()
94+
95+
projectID, projectIDExists := tt.Meta.scwClient.GetDefaultProjectID()
96+
if !projectIDExists {
97+
t.Skip("no default project ID")
98+
}
99+
100+
resource.ParallelTest(t, resource.TestCase{
101+
PreCheck: func() { testAccPreCheck(t) },
102+
ProviderFactories: tt.ProviderFactories,
103+
Steps: []resource.TestStep{
104+
{
105+
Config: `data scaleway_account_project "project" {}`,
106+
Check: resource.ComposeTestCheckFunc(
107+
resource.TestCheckResourceAttr("data.scaleway_account_project.project", "id", projectID),
108+
resource.TestCheckResourceAttrSet("data.scaleway_account_project.project", "name"),
109+
),
110+
},
111+
},
112+
})
113+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: ""
6+
form: {}
7+
headers:
8+
User-Agent:
9+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
10+
terraform/terraform-tests
11+
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
12+
method: GET
13+
response:
14+
body: '{"created_at":"2019-09-30T07:52:49.358300Z","description":"","id":"105bdce1-64c0-48ab-899d-868455867ecf","name":"default","organization_id":"105bdce1-64c0-48ab-899d-868455867ecf","updated_at":"2020-05-03T19:41:17.997124Z"}'
15+
headers:
16+
Content-Length:
17+
- "227"
18+
Content-Security-Policy:
19+
- default-src 'none'; frame-ancestors 'none'
20+
Content-Type:
21+
- application/json
22+
Date:
23+
- Fri, 07 Apr 2023 15:18:25 GMT
24+
Server:
25+
- Scaleway API-Gateway
26+
Strict-Transport-Security:
27+
- max-age=63072000
28+
X-Content-Type-Options:
29+
- nosniff
30+
X-Frame-Options:
31+
- DENY
32+
X-Request-Id:
33+
- 59845239-580d-490d-bd19-a6ba0a7be0c8
34+
status: 200 OK
35+
code: 200
36+
duration: ""
37+
- request:
38+
body: ""
39+
form: {}
40+
headers:
41+
User-Agent:
42+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
43+
terraform/terraform-tests
44+
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
45+
method: GET
46+
response:
47+
body: '{"created_at":"2019-09-30T07:52:49.358300Z","description":"","id":"105bdce1-64c0-48ab-899d-868455867ecf","name":"default","organization_id":"105bdce1-64c0-48ab-899d-868455867ecf","updated_at":"2020-05-03T19:41:17.997124Z"}'
48+
headers:
49+
Content-Length:
50+
- "227"
51+
Content-Security-Policy:
52+
- default-src 'none'; frame-ancestors 'none'
53+
Content-Type:
54+
- application/json
55+
Date:
56+
- Fri, 07 Apr 2023 15:18:25 GMT
57+
Server:
58+
- Scaleway API-Gateway
59+
Strict-Transport-Security:
60+
- max-age=63072000
61+
X-Content-Type-Options:
62+
- nosniff
63+
X-Frame-Options:
64+
- DENY
65+
X-Request-Id:
66+
- 845f0068-149c-436a-b37f-e89f43259352
67+
status: 200 OK
68+
code: 200
69+
duration: ""
70+
- request:
71+
body: ""
72+
form: {}
73+
headers:
74+
User-Agent:
75+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
76+
terraform/terraform-tests
77+
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
78+
method: GET
79+
response:
80+
body: '{"created_at":"2019-09-30T07:52:49.358300Z","description":"","id":"105bdce1-64c0-48ab-899d-868455867ecf","name":"default","organization_id":"105bdce1-64c0-48ab-899d-868455867ecf","updated_at":"2020-05-03T19:41:17.997124Z"}'
81+
headers:
82+
Content-Length:
83+
- "227"
84+
Content-Security-Policy:
85+
- default-src 'none'; frame-ancestors 'none'
86+
Content-Type:
87+
- application/json
88+
Date:
89+
- Fri, 07 Apr 2023 15:18:25 GMT
90+
Server:
91+
- Scaleway API-Gateway
92+
Strict-Transport-Security:
93+
- max-age=63072000
94+
X-Content-Type-Options:
95+
- nosniff
96+
X-Frame-Options:
97+
- DENY
98+
X-Request-Id:
99+
- e68029b0-2e7a-429a-b89d-a2adbb38d627
100+
status: 200 OK
101+
code: 200
102+
duration: ""
103+
- request:
104+
body: ""
105+
form: {}
106+
headers:
107+
User-Agent:
108+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
109+
terraform/terraform-tests
110+
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
111+
method: GET
112+
response:
113+
body: '{"created_at":"2019-09-30T07:52:49.358300Z","description":"","id":"105bdce1-64c0-48ab-899d-868455867ecf","name":"default","organization_id":"105bdce1-64c0-48ab-899d-868455867ecf","updated_at":"2020-05-03T19:41:17.997124Z"}'
114+
headers:
115+
Content-Length:
116+
- "227"
117+
Content-Security-Policy:
118+
- default-src 'none'; frame-ancestors 'none'
119+
Content-Type:
120+
- application/json
121+
Date:
122+
- Fri, 07 Apr 2023 15:18:25 GMT
123+
Server:
124+
- Scaleway API-Gateway
125+
Strict-Transport-Security:
126+
- max-age=63072000
127+
X-Content-Type-Options:
128+
- nosniff
129+
X-Frame-Options:
130+
- DENY
131+
X-Request-Id:
132+
- 01c66dd5-d54d-432f-aefc-5febc7057753
133+
status: 200 OK
134+
code: 200
135+
duration: ""
136+
- request:
137+
body: ""
138+
form: {}
139+
headers:
140+
User-Agent:
141+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
142+
terraform/terraform-tests
143+
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
144+
method: GET
145+
response:
146+
body: '{"created_at":"2019-09-30T07:52:49.358300Z","description":"","id":"105bdce1-64c0-48ab-899d-868455867ecf","name":"default","organization_id":"105bdce1-64c0-48ab-899d-868455867ecf","updated_at":"2020-05-03T19:41:17.997124Z"}'
147+
headers:
148+
Content-Length:
149+
- "227"
150+
Content-Security-Policy:
151+
- default-src 'none'; frame-ancestors 'none'
152+
Content-Type:
153+
- application/json
154+
Date:
155+
- Fri, 07 Apr 2023 15:18:26 GMT
156+
Server:
157+
- Scaleway API-Gateway
158+
Strict-Transport-Security:
159+
- max-age=63072000
160+
X-Content-Type-Options:
161+
- nosniff
162+
X-Frame-Options:
163+
- DENY
164+
X-Request-Id:
165+
- 3b61cccb-76ea-4607-9f4d-9fb08a80c7fb
166+
status: 200 OK
167+
code: 200
168+
duration: ""

0 commit comments

Comments
 (0)