2023-11-24 15:58:21 +01:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
2023-11-27 09:00:08 +01:00
// The provider package implements the Constellation Terraform provider's
// "provider" resource, which is the main entrypoint for Terraform to
// interact with the provider.
2023-11-24 15:58:21 +01:00
package provider
import (
"context"
2023-12-18 14:21:19 +01:00
"fmt"
2023-11-24 15:58:21 +01:00
2023-12-18 14:21:19 +01:00
"github.com/edgelesssys/constellation/v2/internal/semver"
2023-11-27 09:00:08 +01:00
datastruct "github.com/edgelesssys/constellation/v2/terraform-provider-constellation/internal/data"
2023-11-24 15:58:21 +01:00
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
)
// Perform interface cast to ensure ConstellationProvider satisfies various provider interfaces.
var _ provider . Provider = & ConstellationProvider { }
2023-11-27 09:00:08 +01:00
// ConstellationProviderModel is the provider data model.
type ConstellationProviderModel struct { }
2023-11-24 15:58:21 +01:00
// ConstellationProvider is the provider implementation.
type ConstellationProvider struct {
2023-12-14 15:47:55 +01:00
// version is set to the provider version on release, and the pseudo version on local builds. The pseudo version is not a valid default for the image_version attribute.
2023-11-24 15:58:21 +01:00
version string
}
2023-11-27 09:00:08 +01:00
// New creates a new provider, based on a version.
func New ( version string ) func ( ) provider . Provider {
return func ( ) provider . Provider {
return & ConstellationProvider {
version : version ,
}
}
2023-11-24 15:58:21 +01:00
}
// Metadata returns the Providers name and version upon request.
func ( p * ConstellationProvider ) Metadata ( _ context . Context , _ provider . MetadataRequest , resp * provider . MetadataResponse ) {
resp . TypeName = "constellation"
resp . Version = p . version
}
// Schema defines the HCL schema of the provider, i.e. what attributes it has and what they are used for.
func ( p * ConstellationProvider ) Schema ( _ context . Context , _ provider . SchemaRequest , resp * provider . SchemaResponse ) {
resp . Schema = schema . Schema {
2023-12-14 15:47:55 +01:00
Description : "The Constellation provider manages Constellation clusters." ,
MarkdownDescription : ` The Constellation provider manages Constellation clusters .
Given user - defined infrastructure in Terraform , the provider with its main ' constellation_cluster ' resource manages the entire lifecycle of a cluster .
The provider allows easy usage of custom infrastructure setups and GitOps workflows .
It is released as part of Constellation releases , such that each provider version is compatible with the corresponding Constellation version . ` ,
2023-11-24 15:58:21 +01:00
}
}
// Configure is called when the provider block is initialized, and conventionally
// used to setup any API clients or other resources required for the provider.
func ( p * ConstellationProvider ) Configure ( ctx context . Context , req provider . ConfigureRequest , resp * provider . ConfigureResponse ) {
// Populate the provider configuration model with what the user supplied when
2023-11-27 09:00:08 +01:00
// declaring the provider block. No-op for now, as no attributes are defined.
2023-11-24 15:58:21 +01:00
var data ConstellationProviderModel
resp . Diagnostics . Append ( req . Config . Get ( ctx , & data ) ... )
if resp . Diagnostics . HasError ( ) {
return
}
2023-12-18 14:21:19 +01:00
ver , err := semver . New ( p . version )
if err != nil {
resp . Diagnostics . AddError ( "Invalid provider version" ,
fmt . Sprintf ( "Expected a valid semantic version, got %s: %s" , p . version , err ) ,
)
return
}
2023-12-14 15:47:55 +01:00
config := datastruct . ProviderData {
2023-12-18 14:21:19 +01:00
Version : ver ,
2023-12-14 15:47:55 +01:00
}
2023-11-24 15:58:21 +01:00
2023-11-27 09:00:08 +01:00
// Make the clients available during data source and resource "Configure" methods.
resp . DataSourceData = config
resp . ResourceData = config
2023-11-24 15:58:21 +01:00
}
// Resources lists the resources implemented by the provider.
func ( p * ConstellationProvider ) Resources ( _ context . Context ) [ ] func ( ) resource . Resource {
return [ ] func ( ) resource . Resource {
2023-12-05 16:16:50 +01:00
NewClusterResource ,
2023-11-24 15:58:21 +01:00
}
}
// DataSources lists the data sources implemented by the provider.
func ( p * ConstellationProvider ) DataSources ( _ context . Context ) [ ] func ( ) datasource . DataSource {
return [ ] func ( ) datasource . DataSource {
2023-11-28 17:30:11 +01:00
NewImageDataSource , NewAttestationDataSource ,
2023-11-24 15:58:21 +01:00
}
}