-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iam): add iam application datasource (#1348)
* feat(iam): application datasource ok + tests * feat(iam): application: removed optional description from data source
- Loading branch information
Showing
4 changed files
with
1,727 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package scaleway | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
func dataSourceScalewayIamApplication() *schema.Resource { | ||
// Generate datasource schema from resource | ||
dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayIamApplication().Schema) | ||
|
||
addOptionalFieldsToSchema(dsSchema, "name") | ||
|
||
dsSchema["name"].ConflictsWith = []string{"application_id"} | ||
dsSchema["application_id"] = &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The ID of the IAM application", | ||
ConflictsWith: []string{"name"}, | ||
ValidateFunc: validationUUID(), | ||
} | ||
// Default organization_id will be available on a major release. Please check #1337 | ||
dsSchema["organization_id"] = &schema.Schema{ | ||
Type: schema.TypeString, | ||
Description: "The organization_id you want to attach the resource to", | ||
Required: true, | ||
} | ||
|
||
return &schema.Resource{ | ||
ReadContext: dataSourceScalewayIamApplicationRead, | ||
Schema: dsSchema, | ||
} | ||
} | ||
|
||
func dataSourceScalewayIamApplicationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
api := iamAPI(meta) | ||
|
||
appID, appIDExists := d.GetOk("application_id") | ||
|
||
if !appIDExists { | ||
res, err := api.ListApplications(&iam.ListApplicationsRequest{ | ||
OrganizationID: expandStringPtr(d.Get("organization_id")), | ||
Name: expandStringPtr(d.Get("name")), | ||
}, scw.WithContext(ctx)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
for _, app := range res.Applications { | ||
if app.Name == d.Get("name").(string) { | ||
if appID != "" { | ||
return diag.Errorf("more than 1 application found with the same name %s", d.Get("name")) | ||
} | ||
appID = app.ID | ||
} | ||
} | ||
if appID == "" { | ||
return diag.Errorf("no application found with the name %s", d.Get("name")) | ||
} | ||
} | ||
|
||
d.SetId(appID.(string)) | ||
err := d.Set("application_id", appID) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
diags := resourceScalewayIamApplicationRead(ctx, d, meta) | ||
if diags != nil { | ||
return append(diags, diag.Errorf("failed to read iam application state")...) | ||
} | ||
|
||
if d.Id() == "" { | ||
return diag.Errorf("iam application (%s) not found", appID) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package scaleway | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccScalewayDataSourceIamApplication_Basic(t *testing.T) { | ||
SkipBetaTest(t) | ||
tt := NewTestTools(t) | ||
defer tt.Cleanup() | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: tt.ProviderFactories, | ||
CheckDestroy: resource.ComposeTestCheckFunc( | ||
testAccCheckScalewayIamApplicationDestroy(tt), | ||
), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
resource "scaleway_iam_application" "app_ds_basic" { | ||
name = "test_data_source_basic" | ||
} | ||
`, | ||
}, | ||
{ | ||
Config: ` | ||
resource "scaleway_iam_application" "app_ds_basic" { | ||
name = "test_data_source_basic" | ||
} | ||
data "scaleway_iam_application" "find_by_id_basic" { | ||
application_id = scaleway_iam_application.app_ds_basic.id | ||
organization_id = "08555df8-bb26-43bc-b749-1b98c5d02343" | ||
} | ||
data "scaleway_iam_application" "find_by_name_basic" { | ||
name = "test_data_source_basic" | ||
organization_id = "08555df8-bb26-43bc-b749-1b98c5d02343" | ||
} | ||
`, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckScalewayIamApplicationExists(tt, "scaleway_iam_application.app_ds_basic"), | ||
resource.TestCheckResourceAttr("data.scaleway_iam_application.find_by_id_basic", "name", "test_data_source_basic"), | ||
resource.TestCheckResourceAttr("data.scaleway_iam_application.find_by_name_basic", "name", "test_data_source_basic"), | ||
resource.TestCheckResourceAttrPair("data.scaleway_iam_application.find_by_id_basic", "id", "scaleway_iam_application.app_ds_basic", "id"), | ||
resource.TestCheckResourceAttrPair("data.scaleway_iam_application.find_by_name_basic", "id", "scaleway_iam_application.app_ds_basic", "id"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
resource "scaleway_iam_application" "app_ds_basic" { | ||
name = "test_data_source_basic_renamed" | ||
description = "test_data_source_basic_description" | ||
} | ||
`, | ||
}, | ||
{ | ||
Config: ` | ||
resource "scaleway_iam_application" "app_ds_basic" { | ||
name = "test_data_source_basic_renamed" | ||
description = "test_data_source_basic_description" | ||
} | ||
data "scaleway_iam_application" "find_by_id_basic" { | ||
application_id = scaleway_iam_application.app_ds_basic.id | ||
organization_id = "08555df8-bb26-43bc-b749-1b98c5d02343" | ||
} | ||
data "scaleway_iam_application" "find_by_name_basic" { | ||
name = "test_data_source_basic_renamed" | ||
organization_id = "08555df8-bb26-43bc-b749-1b98c5d02343" | ||
} | ||
`, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckScalewayIamApplicationExists(tt, "scaleway_iam_application.app_ds_basic"), | ||
resource.TestCheckResourceAttr("data.scaleway_iam_application.find_by_id_basic", "name", "test_data_source_basic_renamed"), | ||
resource.TestCheckResourceAttr("data.scaleway_iam_application.find_by_name_basic", "name", "test_data_source_basic_renamed"), | ||
resource.TestCheckResourceAttr("data.scaleway_iam_application.find_by_id_basic", "description", "test_data_source_basic_description"), | ||
resource.TestCheckResourceAttr("data.scaleway_iam_application.find_by_name_basic", "description", "test_data_source_basic_description"), | ||
resource.TestCheckResourceAttrPair("data.scaleway_iam_application.find_by_id_basic", "id", "scaleway_iam_application.app_ds_basic", "id"), | ||
resource.TestCheckResourceAttrPair("data.scaleway_iam_application.find_by_name_basic", "id", "scaleway_iam_application.app_ds_basic", "id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.