2022-12-08 16:09:31 +00:00
|
|
|
package garage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
garage "git.deuxfleurs.fr/garage-sdk/garage-admin-sdk-golang"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
|
|
)
|
|
|
|
|
2022-12-10 16:48:33 +00:00
|
|
|
type garageProvider struct {
|
|
|
|
client *garage.APIClient
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateContext(tfCtx context.Context, p *garageProvider) context.Context {
|
|
|
|
return context.WithValue(tfCtx, garage.ContextAccessToken, p.ctx.Value(garage.ContextAccessToken))
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:09:31 +00:00
|
|
|
// Provider -
|
|
|
|
func Provider() *schema.Provider {
|
|
|
|
return &schema.Provider{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"host": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("GARAGE_HOST", nil),
|
|
|
|
},
|
|
|
|
"scheme": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("GARAGE_SCHEME", "https"),
|
|
|
|
},
|
|
|
|
"token": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Sensitive: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("GARAGE_TOKEN", nil),
|
|
|
|
},
|
|
|
|
},
|
2022-12-10 16:48:33 +00:00
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
|
|
|
"garage_bucket": resourceBucket(),
|
|
|
|
"garage_bucket_global_alias": resourceBucketGlobalAlias(),
|
|
|
|
"garage_bucket_key": resourceBucketKey(),
|
|
|
|
"garage_bucket_local_alias": resourceBucketLocalAlias(),
|
|
|
|
"garage_key": resourceKey(),
|
|
|
|
},
|
2022-12-08 16:09:31 +00:00
|
|
|
DataSourcesMap: map[string]*schema.Resource{},
|
|
|
|
ConfigureContextFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
|
|
|
|
host := d.Get("host").(string)
|
|
|
|
scheme := d.Get("scheme").(string)
|
|
|
|
token := d.Get("token").(string)
|
|
|
|
|
|
|
|
var diags diag.Diagnostics
|
|
|
|
|
|
|
|
if host == "" || token == "" {
|
|
|
|
diags = append(diags, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: "Unable to find host or token",
|
|
|
|
Detail: "Those values must be set",
|
|
|
|
})
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add more configuration values
|
|
|
|
configuration := garage.NewConfiguration()
|
|
|
|
configuration.Host = host
|
|
|
|
configuration.Scheme = scheme
|
|
|
|
|
|
|
|
client := garage.NewAPIClient(configuration)
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, garage.ContextAccessToken, token)
|
|
|
|
|
2022-12-10 16:48:33 +00:00
|
|
|
return &garageProvider{
|
|
|
|
client: client,
|
|
|
|
ctx: ctx,
|
|
|
|
}, diags
|
2022-12-08 16:09:31 +00:00
|
|
|
}
|