Skip to content

Commit

Permalink
feat(organization): application users support (#1508)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Savciuc <[email protected]>
  • Loading branch information
Serpentiel and ivan-savciuc authored Jan 6, 2024
1 parent 5a437dc commit e124fbc
Show file tree
Hide file tree
Showing 24 changed files with 1,145 additions and 194 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ nav_order: 1
<!-- [MAJOR.MINOR.PATCH] - YYYY-MM-DD -->

## [MAJOR.MINOR.PATCH] - YYYY-MM-DD
- Add organization application users support

## [4.12.1] - 2024-01-05

Expand Down
26 changes: 26 additions & 0 deletions docs/data-sources/organization_application_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "aiven_organization_application_user Data Source - terraform-provider-aiven"
subcategory: ""
description: |-
Retrieves information about an organization application user from Aiven.
---

# aiven_organization_application_user (Data Source)

Retrieves information about an organization application user from Aiven.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `organization_id` (String) Identifier of the organization the application user belongs to.
- `user_id` (String) Identifier of the organization application user.

### Read-Only

- `email` (String) Email of the organization application user.
- `name` (String) Name of the organization application user.
41 changes: 41 additions & 0 deletions docs/resources/organization_application_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "aiven_organization_application_user Resource - terraform-provider-aiven"
subcategory: ""
description: |-
Creates and manages an organization application user in Aiven. Please note that this resource is in beta and may change without notice. To use this resource, please set the PROVIDERAIVENENABLE_BETA environment variable.
---

# aiven_organization_application_user (Resource)

Creates and manages an organization application user in Aiven. Please note that this resource is in beta and may change without notice. To use this resource, please set the PROVIDER_AIVEN_ENABLE_BETA environment variable.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the organization application user.
- `organization_id` (String) Identifier of the organization the application user belongs to.

### Optional

- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `email` (String) Email of the organization application user.
- `id` (String) Compound identifier of the organization application user.
- `user_id` (String) Identifier of the organization application user.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs.
- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Read operations occur during any refresh or planning operation when refresh is enabled.
- `update` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
8 changes: 6 additions & 2 deletions docs/resources/organization_group_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "aiven_organization_group_project Resource - terraform-provider-aiven"
subcategory: ""
description: |-
Creates and manages an organization group project relations in Aiven.
Creates and manages an organization group project relations in Aiven. Please note that this resource is in beta and may change without notice. To use this resource, please set the PROVIDERAIVENENABLE_BETA environment variable.
---

# aiven_organization_group_project (Resource)

Creates and manages an organization group project relations in Aiven.
Creates and manages an organization group project relations in Aiven. Please note that this resource is in beta and may change without notice. To use this resource, please set the PROVIDER_AIVEN_ENABLE_BETA environment variable.



Expand All @@ -25,6 +25,10 @@ Creates and manages an organization group project relations in Aiven.

- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `id` (String) Compound identifier of the organization group project relation.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Expand Down
4 changes: 2 additions & 2 deletions docs/resources/organization_user_group_member.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "aiven_organization_user_group_member Resource - terraform-provider-aiven"
subcategory: ""
description: |-
Creates and manages an organization user group members in Aiven. Please no that this resource is in beta and may change without notice. To use it please use the beta environment variable PROVIDERAIVENENABLE_BETA.
Creates and manages an organization user group members in Aiven. Please note that this resource is in beta and may change without notice. To use this resource, please set the PROVIDERAIVENENABLE_BETA environment variable.
---

# aiven_organization_user_group_member (Resource)

Creates and manages an organization user group members in Aiven. Please no that this resource is in beta and may change without notice. To use it please use the beta environment variable PROVIDER_AIVEN_ENABLE_BETA.
Creates and manages an organization user group members in Aiven. Please note that this resource is in beta and may change without notice. To use this resource, please set the PROVIDER_AIVEN_ENABLE_BETA environment variable.



Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/aiven/terraform-provider-aiven
go 1.21

require (
github.com/aiven/aiven-go-client/v2 v2.8.0
github.com/aiven/aiven-go-client/v2 v2.9.0
github.com/avast/retry-go v3.0.0+incompatible
github.com/dave/jennifer v1.7.0
github.com/docker/go-units v0.5.0
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjA
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/aiven/aiven-go-client/v2 v2.6.0 h1:mSWs0rqkvt3BZ6ljX8H0paSyMkiFU7hptTYBmUmB6E0=
github.com/aiven/aiven-go-client/v2 v2.6.0/go.mod h1:x0xhzxWEKAwKv0xY5FvECiI6tesWshcPHvjwl0B/1SU=
github.com/aiven/aiven-go-client/v2 v2.6.1-0.20240102144320-a7653ed76349 h1:8yrp+pihD3pLLqttT75m1aa0JRIfSk3xbdpDsRj4Jjw=
github.com/aiven/aiven-go-client/v2 v2.6.1-0.20240102144320-a7653ed76349/go.mod h1:x0xhzxWEKAwKv0xY5FvECiI6tesWshcPHvjwl0B/1SU=
github.com/aiven/aiven-go-client/v2 v2.7.0 h1:nvDfzxsELIt+df7zLqePbrI6ZtazmNvhy/8LyKOZ810=
github.com/aiven/aiven-go-client/v2 v2.7.0/go.mod h1:x0xhzxWEKAwKv0xY5FvECiI6tesWshcPHvjwl0B/1SU=
github.com/aiven/aiven-go-client/v2 v2.8.0 h1:1JZVmpp2emjrIWrDiec9+7CqTRyH/qTnhzJxkYUgVlU=
github.com/aiven/aiven-go-client/v2 v2.8.0/go.mod h1:x0xhzxWEKAwKv0xY5FvECiI6tesWshcPHvjwl0B/1SU=
github.com/aiven/aiven-go-client/v2 v2.9.0 h1:LpfilLbahoCzDglh77jbwD5L8sJI3p7BvuZ/NXZoA8w=
github.com/aiven/aiven-go-client/v2 v2.9.0/go.mod h1:x0xhzxWEKAwKv0xY5FvECiI6tesWshcPHvjwl0B/1SU=
github.com/aiven/go-api-schemas v1.51.0 h1:6e9oxSTIhKFixOJV3fbnrpKAULhVxls4U4smZjRE7cU=
github.com/aiven/go-api-schemas v1.51.0/go.mod h1:/bPxBUHza/2Aeer6hIIdB++GxKiw9K1KCBtRa2rtZ5I=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
Expand Down
72 changes: 72 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/plugin/errmsg"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/server"
)
Expand All @@ -29,6 +30,7 @@ var (
}
)

// GetTestAivenClient returns a new Aiven client that can be used for acceptance tests.
func GetTestAivenClient() *aiven.Client {
testAivenClientOnce.Do(func() {
client, err := common.NewAivenClient()
Expand All @@ -40,6 +42,65 @@ func GetTestAivenClient() *aiven.Client {
return testAivenClient
}

// commonTestDependencies is a struct that contains common dependencies that are used by acceptance tests.
type commonTestDependencies struct {
// isBeta is a flag that indicates whether the provider is in beta mode.
isBeta bool
// organizationName is the name of the organization that is used for acceptance tests.
organizationName string
// organizationUserID is the ID of the organization user that is used for acceptance tests.
organizationUserID *string
}

// IsBeta returns a flag that indicates whether the provider is in beta mode.
func (d *commonTestDependencies) IsBeta() bool {
return d.isBeta
}

// OrganizationName returns the name of the organization that is used for acceptance tests.
func (d *commonTestDependencies) OrganizationName() string {
return d.organizationName
}

// OrganizationUserID returns the ID of the organization user that is used for acceptance tests.
func (d *commonTestDependencies) OrganizationUserID() *string {
return d.organizationUserID
}

// CommonTestDependencies returns a new commonTestDependencies struct that contains common dependencies that are
// used by acceptance tests.
// nolint:revive // Ignore unexported type error because this type is not meant to be used outside of this package.
func CommonTestDependencies(t *testing.T) *commonTestDependencies {
// We mimic the real error message that is returned by Terraform when the acceptance tests are skipped.
//
// This is done because the tests that use this function are running it before the real Terraform check takes
// place, and we want to avoid false positively running this function when the acceptance tests are not actually
// ran, e.g. if unit tests are ran instead.
//
// See https://github.com/hashicorp/terraform-plugin-testing/blob/v1.6.0/helper/resource/testing.go#L849-L857 for
// more details on the real check.
if _, ok := os.LookupEnv("TF_ACC"); !ok {
t.Skip("Acceptance tests skipped unless env 'TF_ACC' set")
}

deps := &commonTestDependencies{
isBeta: os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != "",
}

organizationName, ok := os.LookupEnv("AIVEN_ORGANIZATION_NAME")
if !ok {
t.Fatal("AIVEN_ORGANIZATION_NAME environment variable must be set for acceptance tests.")
}
deps.organizationName = organizationName

organizationUserID, ok := os.LookupEnv("AIVEN_ORGANIZATION_USER_ID")
if ok {
deps.organizationUserID = &organizationUserID
}

return deps
}

const (
// DefaultResourceNamePrefix is the default prefix used for resource names in acceptance tests.
DefaultResourceNamePrefix = "test-acc"
Expand All @@ -60,6 +121,7 @@ func TestAccPreCheck(t *testing.T) {
}
}

// TestAccCheckAivenServiceResourceDestroy verifies that the given service is destroyed.
func TestAccCheckAivenServiceResourceDestroy(s *terraform.State) error {
c := GetTestAivenClient()

Expand Down Expand Up @@ -112,3 +174,13 @@ func TestAccCheckAivenServiceResourceDestroy(s *terraform.State) error {

return nil
}

// ResourceFromState returns a resource state from the given Terraform state.
func ResourceFromState(state *terraform.State, name string) (*terraform.ResourceState, error) {
rs, ok := state.RootModule().Resources[name]
if !ok {
return nil, fmt.Errorf(errmsg.ResourceNotFound, name)
}

return rs, nil
}
19 changes: 19 additions & 0 deletions internal/plugin/errmsg/errmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var (
// DetailErrorUpdatingResource is the detailed error message for when a resource cannot be updated.
DetailErrorUpdatingResource = "An unexpected error occurred while updating the resource (%s): %s."

// DetailErrorUpdatingResourceNotSupported is the detailed error message for when a resource cannot be updated.
DetailErrorUpdatingResourceNotSupported = "Updating the resource (%s) is not supported."

// DetailErrorDeletingResource is the detailed error message for when a resource cannot be deleted.
DetailErrorDeletingResource = "An unexpected error occurred while deleting the resource (%s): %s."

Expand All @@ -90,3 +93,19 @@ var (
// DetailErrorReadingDataSource is the detailed error message for when a data source cannot be read.
DetailErrorReadingDataSource = "An unexpected error occurred while reading the data source (%s): %s."
)

// Below is the list of classic Go-style error messages that are used in the provider.
// The classic Go-style error messages are used to provide more information about the error.
// The classic Go-style error messages should start with a lowercase letter and SHOULD NOT end with a period.
// The classic Go-style error messages may contain placeholders for values that are not known at the time of writing.
//
// See Go error handling for more information:
// https://blog.golang.org/error-handling-and-go.
var (
// ResourceNotFound is the error message for when a resource cannot be found.
// This error is intended to be used in acceptance tests.
ResourceNotFound = "resource not found: %s"

// AivenResourceNotFound is the error message for when an Aiven resource cannot be found.
AivenResourceNotFound = "aiven resource %s with compound ID %s not found"
)
29 changes: 24 additions & 5 deletions internal/plugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,46 @@ func (p *AivenProvider) Configure(

// Resources returns the resources supported by this provider.
func (p *AivenProvider) Resources(context.Context) []func() resource.Resource {
isBeta := os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != ""

// List of resources that are currently available in the provider.
resources := []func() resource.Resource{
organization.NewOrganizationResource,
}

isBeta := os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != ""

// Add to a list of resources that are currently in beta.
if isBeta {
resources = append(resources, organization.NewOrganizationUserGroupMembersResource)
resources = append(resources, organization.NewOrganizationGroupProjectResource)
betaResources := []func() resource.Resource{
organization.NewOrganizationUserGroupMembersResource,
organization.NewOrganizationGroupProjectResource,
organization.NewOrganizationApplicationUser,
}

resources = append(resources, betaResources...)
}

return resources
}

// DataSources returns the data sources supported by this provider.
func (p *AivenProvider) DataSources(context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
// List of data sources that are currently available in the provider.
dataSources := []func() datasource.DataSource{
organization.NewOrganizationDataSource,
}

isBeta := os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != ""

// Add to a list of data sources that are currently in beta.
if isBeta {
betaDataSources := []func() datasource.DataSource{
organization.NewOrganizationApplicationUserDataSource,
}

dataSources = append(dataSources, betaDataSources...)
}

return dataSources
}

// New returns a new provider factory for the Aiven provider.
Expand Down
Loading

0 comments on commit e124fbc

Please sign in to comment.