-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/ip-reverse-lookup
# Conflicts: # internal/provider/provider_test.go
- Loading branch information
Showing
14 changed files
with
288 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
PUBLIC_CLOUD_API_SPEC_URL=https://raw.githubusercontent.com/Leaseweb/leaseweb-go-sdk/main/publicCloud/api/openapi.yaml | ||
DEDICATED_SERVER_API_SPEC_URL=https://raw.githubusercontent.com/Leaseweb/leaseweb-go-sdk/main/dedicatedServer/api/openapi.yaml | ||
PUBLIC_CLOUD_API_SPEC_URL=https://raw.githubusercontent.com/Leaseweb/leaseweb-go-sdk/main/publiccloud/api/openapi.yaml | ||
DEDICATED_SERVER_API_SPEC_URL=https://raw.githubusercontent.com/Leaseweb/leaseweb-go-sdk/main/dedicatedserver/api/openapi.yaml | ||
#PUBLIC_CLOUD_API_SPEC_URL=http://host.docker.internal:8081/publicCloud.json | ||
#DEDICATED_SERVER_API_SPEC_URL=http://host.docker.internal:8081/dedicatedServer.json |
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
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,33 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "leaseweb_public_cloud_isos Data Source - leaseweb" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# leaseweb_public_cloud_isos (Data Source) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
# List all Public Cloud isos | ||
data "leaseweb_public_cloud_isos" "all" {} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `isos` (Attributes List) (see [below for nested schema](#nestedatt--isos)) | ||
|
||
<a id="nestedatt--isos"></a> | ||
### Nested Schema for `isos` | ||
|
||
Read-Only: | ||
|
||
- `id` (String) ISO ID | ||
- `name` (String) The name of ISO |
2 changes: 2 additions & 0 deletions
2
examples/data-sources/leaseweb_public_cloud_isos/data-source.tf
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,2 @@ | ||
# List all Public Cloud isos | ||
data "leaseweb_public_cloud_isos" "all" {} |
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
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
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,167 @@ | ||
package publiccloud | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" | ||
"github.com/leaseweb/leaseweb-go-sdk/v2/publiccloud" | ||
"github.com/leaseweb/terraform-provider-leaseweb/internal/provider/client" | ||
"github.com/leaseweb/terraform-provider-leaseweb/internal/utils" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSourceWithConfigure = &ISOsDataSource{} | ||
) | ||
|
||
type ISODataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Name types.String `tfsdk:"name"` | ||
} | ||
|
||
type ISOsDataSourceModel struct { | ||
ISOs []ISODataSourceModel `tfsdk:"isos"` | ||
} | ||
|
||
func adaptIsosToISOsDataSource(sdkISOs []publiccloud.Iso) ISOsDataSourceModel { | ||
var ISOs ISOsDataSourceModel | ||
|
||
for _, sdkISO := range sdkISOs { | ||
ISO := adaptIsoToISODataSource(sdkISO) | ||
ISOs.ISOs = append(ISOs.ISOs, ISO) | ||
} | ||
|
||
return ISOs | ||
} | ||
|
||
func adaptIsoToISODataSource(sdkISO publiccloud.Iso) ISODataSourceModel { | ||
return ISODataSourceModel{ | ||
ID: basetypes.NewStringValue(sdkISO.GetId()), | ||
Name: basetypes.NewStringValue(sdkISO.GetName()), | ||
} | ||
} | ||
|
||
func getISOs( | ||
ctx context.Context, | ||
api publiccloud.PubliccloudAPI, | ||
) ([]publiccloud.Iso, *http.Response, error) { | ||
var isos []publiccloud.Iso | ||
var offset *int32 | ||
|
||
request := api.GetIsoList(ctx) | ||
|
||
for { | ||
result, httpResponse, err := request.Execute() | ||
if err != nil { | ||
return nil, httpResponse, fmt.Errorf("getISOs: %w", err) | ||
} | ||
isos = append(isos, result.Isos...) | ||
|
||
metadata := result.GetMetadata() | ||
|
||
offset = utils.NewOffset( | ||
metadata.GetLimit(), | ||
metadata.GetOffset(), | ||
metadata.GetTotalCount(), | ||
) | ||
if offset == nil { | ||
break | ||
} | ||
|
||
request = request.Offset(*offset) | ||
} | ||
|
||
return isos, nil, nil | ||
} | ||
|
||
type ISOsDataSource struct { | ||
name string | ||
client publiccloud.PubliccloudAPI | ||
} | ||
|
||
func (i *ISOsDataSource) Metadata( | ||
_ context.Context, | ||
request datasource.MetadataRequest, | ||
response *datasource.MetadataResponse, | ||
) { | ||
response.TypeName = fmt.Sprintf("%s_%s", request.ProviderTypeName, i.name) | ||
} | ||
|
||
func (i *ISOsDataSource) Schema( | ||
_ context.Context, | ||
_ datasource.SchemaRequest, | ||
response *datasource.SchemaResponse, | ||
) { | ||
response.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"isos": schema.ListNestedAttribute{ | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "ISO ID", | ||
}, | ||
"name": schema.StringAttribute{ | ||
Computed: true, | ||
Description: "The name of ISO", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (i *ISOsDataSource) Read( | ||
ctx context.Context, | ||
_ datasource.ReadRequest, | ||
resp *datasource.ReadResponse, | ||
) { | ||
ISOs, httpResponse, err := getISOs(ctx, i.client) | ||
if err != nil { | ||
utils.Error(ctx, &resp.Diagnostics, fmt.Sprintf("Reading data %s", i.name), err, httpResponse) | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append( | ||
resp.State.Set( | ||
ctx, | ||
adaptIsosToISOsDataSource(ISOs), | ||
)..., | ||
) | ||
} | ||
|
||
func (i *ISOsDataSource) Configure( | ||
_ context.Context, | ||
request datasource.ConfigureRequest, | ||
response *datasource.ConfigureResponse, | ||
) { | ||
if request.ProviderData == nil { | ||
return | ||
} | ||
|
||
coreClient, ok := request.ProviderData.(client.Client) | ||
if !ok { | ||
response.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf( | ||
"Expected provider.Client, got: %T. Please report this issue to the provider developers.", | ||
request.ProviderData, | ||
), | ||
) | ||
return | ||
} | ||
|
||
i.client = coreClient.PubliccloudAPI | ||
} | ||
|
||
func NewISOsDataSource() datasource.DataSource { | ||
return &ISOsDataSource{ | ||
name: "public_cloud_isos", | ||
} | ||
} |
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,31 @@ | ||
package publiccloud | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/leaseweb/leaseweb-go-sdk/v2/publiccloud" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_adaptIsoToISODataSource(t *testing.T) { | ||
sdkISO := publiccloud.Iso{ | ||
Id: "id", | ||
Name: "name", | ||
} | ||
|
||
got := adaptIsoToISODataSource(sdkISO) | ||
|
||
assert.Equal(t, "id", got.ID.ValueString()) | ||
assert.Equal(t, "name", got.Name.ValueString()) | ||
} | ||
|
||
func Test_adaptIsosToISOsDataSource(t *testing.T) { | ||
sdkISOs := []publiccloud.Iso{ | ||
{Id: "id"}, | ||
} | ||
|
||
got := adaptIsosToISOsDataSource(sdkISOs) | ||
|
||
assert.Len(t, got.ISOs, 1) | ||
assert.Equal(t, "id", got.ISOs[0].ID.ValueString()) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.