Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions dev-docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ Models in `internal/models/` define Terraform schema structure with `tfsdk` stru
- New code should use `diag.Diagnostics` directly and the `typehelpers` package functions which all return diagnostics

**Provider Data** (`internal/providerdata/`):
- `providerData` struct holds API clients for resources/data sources, built by `New()`: `API` for the platform API and `IaCAPI` for the IaC API
- Exposed as `api` and `iacAPI` fields in the `apiResource` and `apiDataSource` base types
- `GetForResource()` - Retrieves provider data in resources
- `GetForDataSource()` - Retrieves provider data in data sources
- `providerData` struct holds API clients for resources/data sources, built by `New()`: `PlatformAPI` for the platform API and `IaCAPI` for the IaC API
- `GetAPI[T]()` - Retrieves the API client of type `T` (`*platform.API` or `*iac.API`) from the provider data in a configure request
- Exposed as the `api` field by the generic `apiResource[T]`/`apiDataSource[T]` base types, through the `platformAPIResource`/`platformAPIDataSource` (platform API) and `iacAPIResource`/`iacAPIDataSource` (IaC API) aliases; resources and data sources embed the alias matching the API they use

## Architectural Patterns

Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &accountDataSource{}

type accountDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *accountDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/account_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &accountGroupDataSource{}

type accountGroupDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *accountGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/anthropic_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
)

type anthropicIntegrationDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *anthropicIntegrationDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &bindingDataSource{}

type bindingDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *bindingDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &configurationProfileAccountOwnersDataSource{}

type configurationProfileAccountOwnersDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileAccountOwnersDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/configuration_profile_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &configurationProfileEmailDataSource{}

type configurationProfileEmailDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileEmailDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/configuration_profile_jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &configurationProfileJiraDataSource{}

type configurationProfileJiraDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileJiraDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/configuration_profile_msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &configurationProfileMSTeamsDataSource{}

type configurationProfileMSTeamsDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileMSTeamsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &configurationProfileResourceOwnerDataSource{}

type configurationProfileResourceOwnerDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileResourceOwnerDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/configuration_profile_servicenow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &configurationProfileServiceNowDataSource{}

type configurationProfileServiceNowDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileServiceNowDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/configuration_profile_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &configurationProfileSlackDataSource{}

type configurationProfileSlackDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileSlackDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/configuration_profile_symphony.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &configurationProfileSymphonyDataSource{}

type configurationProfileSymphonyDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *configurationProfileSymphonyDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/datadog_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
)

type datadogIntegrationDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *datadogIntegrationDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
22 changes: 13 additions & 9 deletions internal/datasources/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ import (
"github.com/stacklet/terraform-provider-stacklet/internal/providerdata"
)

// apiDataSource is a datasource based on the API.
type apiDataSource struct {
api *platform.API
iacAPI *iac.API
// apiDataSource is a datasource based on an API client.
type apiDataSource[T providerdata.APIClient] struct {
api T
}

// Configure sets up API access for the datasource.
func (d *apiDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if pd, err := providerdata.GetForDataSource(req); err != nil {
func (d *apiDataSource[T]) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if api, err := providerdata.GetAPI[T]("datasource", req.ProviderData); err != nil {
errors.AddDiagError(&resp.Diagnostics, err)
} else if pd != nil {
d.api = pd.API
d.iacAPI = pd.IaCAPI
} else {
d.api = api
}
}

// platformAPIDataSource is a datasource based on the platform API.
type platformAPIDataSource = apiDataSource[*platform.API]

// iacAPIDataSource is a datasource based on the IaC API.
type iacAPIDataSource = apiDataSource[*iac.API] //nolint:unused
2 changes: 1 addition & 1 deletion internal/datasources/gcp_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &gcpIntegrationDataSource{}

type gcpIntegrationDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *gcpIntegrationDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/gcp_integration_surface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &gcpIntegrationSurfaceDataSource{}

type gcpIntegrationSurfaceDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *gcpIntegrationSurfaceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/msteams_integration_surface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &msteamsIntegrationSurfaceDataSource{}

type msteamsIntegrationSurfaceDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *msteamsIntegrationSurfaceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/notification_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &notificationTemplateDataSource{}

type notificationTemplateDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *notificationTemplateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &platformDataSource{}

type platformDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *platformDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var _ datasource.DataSource = &policyDataSource{}

type policyDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *policyDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/policy_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &policyCollectionDataSource{}

type policyCollectionDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *policyCollectionDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/report_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &reportGroupDataSource{}

type reportGroupDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *reportGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var _ datasource.DataSource = &repositoryDataSource{}

// repositoryDataSource defines the data source implementation.
type repositoryDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *repositoryDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ datasource.DataSource = &roleDataSource{}

type roleDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *roleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/role_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &roleAssignmentsDataSource{}

type roleAssignmentsDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *roleAssignmentsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/saml_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &samlProviderDataSource{}

type samlProviderDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *samlProviderDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/sso_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &ssoGroupDataSource{}

type ssoGroupDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *ssoGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &userDataSource{}

type userDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
2 changes: 1 addition & 1 deletion internal/datasources/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var _ datasource.DataSource = &userGroupDataSource{}

type userGroupDataSource struct {
apiDataSource
platformAPIDataSource
}

func (d *userGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
Expand Down
59 changes: 29 additions & 30 deletions internal/providerdata/providerdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@ package providerdata
import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/stacklet/terraform-provider-stacklet/internal/api/iac"
"github.com/stacklet/terraform-provider-stacklet/internal/api/platform"
)

// providerData holds shared data for available in requests.
type providerData struct {
API *platform.API
IaCAPI *iac.API
PlatformAPI *platform.API
IaCAPI *iac.API
}

// New returns configured provider data.
func New(config platform.ClientConfig, iacConfig iac.ClientConfig) *providerData {
return &providerData{
API: platform.New(config),
IaCAPI: iac.New(iacConfig),
PlatformAPI: platform.New(config),
IaCAPI: iac.New(iacConfig),
}
}

// APIClient is the set of API clients available from provider data.
type APIClient interface {
*platform.API | *iac.API
}

type providerDataError struct {
Kind string
ProviderData any
Expand All @@ -39,30 +41,27 @@ func (e providerDataError) Error() string {
return fmt.Sprintf("Expected *ProviderData, got: %T. Please report this issue to the provider developers.", e.ProviderData)
}

// GetForResource returns provider data for a resource request, or nil if not set.
func GetForResource(req resource.ConfigureRequest) (*providerData, error) {
if req.ProviderData == nil {
return nil, nil
}
if providerData, ok := req.ProviderData.(*providerData); ok {
return providerData, nil
}
return nil, providerDataError{
Kind: "resource",
ProviderData: req.ProviderData,
}
}

// GetForDataSource returns provider data for a data source request, or nil if not set.
func GetForDataSource(req datasource.ConfigureRequest) (*providerData, error) {
if req.ProviderData == nil {
return nil, nil
// GetAPI returns the API client of the requested type from the provider data
// of a configure request, or nil if provider data is not set.
//
// kind describes the type of object being configured, for error reporting.
func GetAPI[T APIClient](kind string, data any) (T, error) {
var api T
if data == nil {
return api, nil
}
if providerData, ok := req.ProviderData.(*providerData); ok {
return providerData, nil
pd, ok := data.(*providerData)
if !ok {
return api, providerDataError{
Kind: kind,
ProviderData: data,
}
}
return nil, providerDataError{
Kind: "datasource",
ProviderData: req.ProviderData,
switch p := any(&api).(type) {
case **platform.API:
*p = pd.PlatformAPI
case **iac.API:
*p = pd.IaCAPI
}
return api, nil
}
2 changes: 1 addition & 1 deletion internal/resources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

type accountResource struct {
apiResource
platformAPIResource
}

func (r *accountResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down
Loading
Loading