|
| 1 | +package cloudstack |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework-validators/providervalidator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/provider" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/provider/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | +) |
| 17 | + |
| 18 | +type CloudstackProvider struct{} |
| 19 | + |
| 20 | +type CloudstackProviderModel struct { |
| 21 | + ApiUrl types.String `tfsdk:"api_url"` |
| 22 | + ApiKey types.String `tfsdk:"api_key"` |
| 23 | + SecretKey types.String `tfsdk:"secret_key"` |
| 24 | + Config types.String `tfsdk:"config"` |
| 25 | + Profile types.String `tfsdk:"profile"` |
| 26 | + HttpGetOnly types.Bool `tfsdk:"http_get_only"` |
| 27 | + Timeout types.Int64 `tfsdk:"timeout"` |
| 28 | +} |
| 29 | + |
| 30 | +var _ provider.Provider = (*CloudstackProvider)(nil) |
| 31 | + |
| 32 | +func New() provider.Provider { |
| 33 | + return &CloudstackProvider{} |
| 34 | +} |
| 35 | + |
| 36 | +func (p *CloudstackProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { |
| 37 | + resp.TypeName = "cloudstack" |
| 38 | +} |
| 39 | + |
| 40 | +func (p *CloudstackProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { |
| 41 | + resp.Schema = schema.Schema{ |
| 42 | + Attributes: map[string]schema.Attribute{ |
| 43 | + "api_url": schema.StringAttribute{ |
| 44 | + Optional: true, |
| 45 | + }, |
| 46 | + "api_key": schema.StringAttribute{ |
| 47 | + Optional: true, |
| 48 | + Sensitive: true, |
| 49 | + }, |
| 50 | + "secret_key": schema.StringAttribute{ |
| 51 | + Optional: true, |
| 52 | + Sensitive: true, |
| 53 | + }, |
| 54 | + "config": schema.StringAttribute{ |
| 55 | + Optional: true, |
| 56 | + }, |
| 57 | + "profile": schema.StringAttribute{ |
| 58 | + Optional: true, |
| 59 | + }, |
| 60 | + "http_get_only": schema.BoolAttribute{ |
| 61 | + Optional: true, |
| 62 | + }, |
| 63 | + "timeout": schema.Int64Attribute{ |
| 64 | + Optional: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (p *CloudstackProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { |
| 71 | + apiUrl := os.Getenv("CLOUDSTACK_API_URL") |
| 72 | + apiKey := os.Getenv("CLOUDSTACK_API_KEY") |
| 73 | + secretKey := os.Getenv("CLOUDSTACK_SECRET_KEY") |
| 74 | + httpGetOnly, _ := strconv.ParseBool(os.Getenv("CLOUDSTACK_HTTP_GET_ONLY")) |
| 75 | + timeout, _ := strconv.ParseInt(os.Getenv("CLOUDSTACK_TIMEOUT"), 2, 64) |
| 76 | + |
| 77 | + var data CloudstackProviderModel |
| 78 | + |
| 79 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 80 | + |
| 81 | + if data.ApiUrl.ValueString() != "" { |
| 82 | + apiUrl = data.ApiUrl.ValueString() |
| 83 | + } |
| 84 | + |
| 85 | + if data.ApiKey.ValueString() != "" { |
| 86 | + apiKey = data.ApiKey.ValueString() |
| 87 | + } |
| 88 | + |
| 89 | + if data.SecretKey.ValueString() != "" { |
| 90 | + secretKey = data.SecretKey.ValueString() |
| 91 | + } |
| 92 | + |
| 93 | + if data.HttpGetOnly.ValueBool() { |
| 94 | + httpGetOnly = true |
| 95 | + } |
| 96 | + |
| 97 | + if data.Timeout.ValueInt64() != 0 { |
| 98 | + timeout = data.Timeout.ValueInt64() |
| 99 | + } |
| 100 | + |
| 101 | + cfg := Config{ |
| 102 | + APIURL: apiUrl, |
| 103 | + APIKey: apiKey, |
| 104 | + SecretKey: secretKey, |
| 105 | + HTTPGETOnly: httpGetOnly, |
| 106 | + Timeout: timeout, |
| 107 | + } |
| 108 | + |
| 109 | + client, err := cfg.NewClient() |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + resp.Diagnostics.AddError("cloudstack", fmt.Sprintf("failed to create client: %T", err)) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + resp.ResourceData = client |
| 117 | + resp.DataSourceData = client |
| 118 | +} |
| 119 | + |
| 120 | +func (p *CloudstackProvider) ConfigValidators(ctx context.Context) []provider.ConfigValidator { |
| 121 | + return []provider.ConfigValidator{ |
| 122 | + providervalidator.Conflicting( |
| 123 | + path.MatchRoot("api_url"), |
| 124 | + path.MatchRoot("config"), |
| 125 | + ), |
| 126 | + providervalidator.Conflicting( |
| 127 | + path.MatchRoot("api_url"), |
| 128 | + path.MatchRoot("profile"), |
| 129 | + ), |
| 130 | + providervalidator.Conflicting( |
| 131 | + path.MatchRoot("api_key"), |
| 132 | + path.MatchRoot("config"), |
| 133 | + ), |
| 134 | + providervalidator.Conflicting( |
| 135 | + path.MatchRoot("api_key"), |
| 136 | + path.MatchRoot("profile"), |
| 137 | + ), |
| 138 | + providervalidator.Conflicting( |
| 139 | + path.MatchRoot("secret_key"), |
| 140 | + path.MatchRoot("config"), |
| 141 | + ), |
| 142 | + providervalidator.Conflicting( |
| 143 | + path.MatchRoot("secret_key"), |
| 144 | + path.MatchRoot("profile"), |
| 145 | + ), |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func (p *CloudstackProvider) Resources(ctx context.Context) []func() resource.Resource { |
| 150 | + return []func() resource.Resource{} |
| 151 | +} |
| 152 | + |
| 153 | +func (p *CloudstackProvider) DataSources(ctx context.Context) []func() datasource.DataSource { |
| 154 | + return []func() datasource.DataSource{} |
| 155 | +} |
0 commit comments