Skip to content

Commit

Permalink
feat(account): extract project id if not given (#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep authored Apr 7, 2023
1 parent d8d5a98 commit 59d63aa
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 5 deletions.
19 changes: 14 additions & 5 deletions scaleway/data_source_account_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func dataSourceScalewayAccountProject() *schema.Resource {
dsSchema["name"].ConflictsWith = []string{"project_id"}
dsSchema["project_id"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The ID of the SSH key",
ValidateFunc: validationUUID(),
Expand All @@ -30,22 +31,23 @@ func dataSourceScalewayAccountProject() *schema.Resource {
func dataSourceScalewayAccountProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
accountAPI := accountV2API(meta)

projectID, projectIDExists := d.GetOk("project_id")
if !projectIDExists {
var projectID string

if name, nameExists := d.GetOk("name"); nameExists {
orgID := getOrganizationID(meta, d)
if orgID == nil {
// required not in schema as we could use default
return diag.Errorf("organization_id is required with name")
}
res, err := accountAPI.ListProjects(&accountV2.ListProjectsRequest{
OrganizationID: *orgID,
Name: expandStringPtr(d.Get("name")),
Name: expandStringPtr(name),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
for _, project := range res.Projects {
if project.Name == d.Get("name").(string) {
if project.Name == name {
if projectID != "" {
return diag.Errorf("more than 1 project found with the same name %s", d.Get("name"))
}
Expand All @@ -55,9 +57,16 @@ func dataSourceScalewayAccountProjectRead(ctx context.Context, d *schema.Resourc
if projectID == "" {
diag.Errorf("no project found with the name %s", d.Get("name"))
}
} else {
extractedProjectID, _, err := extractProjectID(d, meta.(*Meta))
if err != nil {
return diag.FromErr(err)
}

projectID = extractedProjectID
}

d.SetId(projectID.(string))
d.SetId(projectID)
_ = d.Set("project_id", projectID)

diags := resourceScalewayAccountProjectRead(ctx, d, meta)
Expand Down
24 changes: 24 additions & 0 deletions scaleway/data_source_account_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,27 @@ func TestAccScalewayDataSourceAccountProject_Default(t *testing.T) {
},
})
}

func TestAccScalewayDataSourceAccountProject_Extract(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()

projectID, projectIDExists := tt.Meta.scwClient.GetDefaultProjectID()
if !projectIDExists {
t.Skip("no default project ID")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Steps: []resource.TestStep{
{
Config: `data scaleway_account_project "project" {}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.scaleway_account_project.project", "id", projectID),
resource.TestCheckResourceAttrSet("data.scaleway_account_project.project", "name"),
),
},
},
})
}
168 changes: 168 additions & 0 deletions scaleway/testdata/data-source-account-project-extract.cassette.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
terraform/terraform-tests
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
method: GET
response:
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"}'
headers:
Content-Length:
- "227"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 07 Apr 2023 15:18:25 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 59845239-580d-490d-bd19-a6ba0a7be0c8
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
terraform/terraform-tests
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
method: GET
response:
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"}'
headers:
Content-Length:
- "227"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 07 Apr 2023 15:18:25 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 845f0068-149c-436a-b37f-e89f43259352
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
terraform/terraform-tests
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
method: GET
response:
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"}'
headers:
Content-Length:
- "227"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 07 Apr 2023 15:18:25 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- e68029b0-2e7a-429a-b89d-a2adbb38d627
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
terraform/terraform-tests
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
method: GET
response:
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"}'
headers:
Content-Length:
- "227"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 07 Apr 2023 15:18:25 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 01c66dd5-d54d-432f-aefc-5febc7057753
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.2; darwin; amd64) terraform-provider/develop
terraform/terraform-tests
url: https://api.scaleway.com/account/v2/projects/105bdce1-64c0-48ab-899d-868455867ecf
method: GET
response:
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"}'
headers:
Content-Length:
- "227"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Fri, 07 Apr 2023 15:18:26 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 3b61cccb-76ea-4607-9f4d-9fb08a80c7fb
status: 200 OK
code: 200
duration: ""

0 comments on commit 59d63aa

Please sign in to comment.