-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from maveonair/project-datasource
Add project as datasource
- Loading branch information
Showing
4 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# incus_project | ||
|
||
Provides information about an Incus project. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "incus_project" "default" { | ||
name = "default" | ||
} | ||
resource "incus_instance" "d1" { | ||
project = data.incus_project.default.name | ||
image = "images:debian/12" | ||
name = "d1" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `name` - **Required** - Name of the project. | ||
|
||
* `remote` - *Optional* - The remote in which the resource was created. If | ||
not provided, the provider's default remote will be used. | ||
|
||
## Attribute Reference | ||
|
||
* `description` - Description of the project. | ||
|
||
* `config` - Map of key/value pairs of | ||
[instance config settings](https://linuxcontainers.org/incus/docs/main/reference/instance_options/). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package project | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
"github.com/lxc/terraform-provider-incus/internal/common" | ||
"github.com/lxc/terraform-provider-incus/internal/errors" | ||
provider_config "github.com/lxc/terraform-provider-incus/internal/provider-config" | ||
) | ||
|
||
type ProjectDataSource struct { | ||
provider *provider_config.IncusProviderConfig | ||
} | ||
|
||
func NewProjectDataSource() datasource.DataSource { | ||
return &ProjectDataSource{} | ||
} | ||
|
||
func (d *ProjectDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = fmt.Sprintf("%s_project", req.ProviderTypeName) | ||
} | ||
|
||
func (d *ProjectDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"name": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"description": schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"config": schema.MapAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
ElementType: types.StringType, | ||
}, | ||
"remote": schema.StringAttribute{ | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *ProjectDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
data := req.ProviderData | ||
if data == nil { | ||
return | ||
} | ||
|
||
provider, ok := data.(*provider_config.IncusProviderConfig) | ||
if !ok { | ||
resp.Diagnostics.Append(errors.NewProviderDataTypeError(req.ProviderData)) | ||
return | ||
} | ||
|
||
d.provider = provider | ||
} | ||
|
||
func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state ProjectModel | ||
|
||
diags := req.Config.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
remote := state.Remote.ValueString() | ||
projectName := state.Name.ValueString() | ||
server, err := d.provider.InstanceServer(remote, projectName, "") | ||
if err != nil { | ||
resp.Diagnostics.Append(errors.NewInstanceServerError(err)) | ||
return | ||
} | ||
|
||
project, _, err := server.GetProject(projectName) | ||
if err != nil { | ||
resp.Diagnostics.AddError(fmt.Sprintf("Failed to retrieve existing project %q", projectName), err.Error()) | ||
return | ||
} | ||
|
||
config, diags := common.ToConfigMapType(ctx, common.ToNullableConfig(project.Config), state.Config) | ||
|
||
state.Name = types.StringValue(project.Name) | ||
state.Description = types.StringValue(project.Description) | ||
state.Config = config | ||
|
||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters