Skip to content

Commit

Permalink
Merge branch 'main' into feat/ip-reverse-lookup
Browse files Browse the repository at this point in the history
# Conflicts:
#	internal/provider/provider_test.go
  • Loading branch information
alrayyes committed Dec 5, 2024
2 parents e56696a + 0ac8a59 commit c5f3aa0
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .env.dist
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.14.0](https://github.com/Leaseweb/terraform-provider-leaseweb/compare/v1.13.0...v1.14.0) (2024-12-03)


### Features

* implement public cloud isos data source ([#188](https://github.com/Leaseweb/terraform-provider-leaseweb/issues/188)) ([ae82e33](https://github.com/Leaseweb/terraform-provider-leaseweb/commit/ae82e339e724117ef3fa013017fe75ddb08ad86c))

## [1.13.0](https://github.com/Leaseweb/terraform-provider-leaseweb/compare/v1.12.1...v1.13.0) (2024-11-29)


Expand Down
2 changes: 1 addition & 1 deletion docker/golang/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.23.3-bookworm AS base
FROM golang:1.23.4-bookworm AS base

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN wget -q -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg \
Expand Down
2 changes: 1 addition & 1 deletion docker/node/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:jod-slim@sha256:4b44c32c9f3118d60977d0dde5f758f63c4f9eac8ddee4275277239ec600950f
FROM node:jod-slim@sha256:a4b757cd491c7f0b57f57951f35f4e85b7e1ad54dbffca4cf9af0725e1650cd8

RUN corepack enable \
&& corepack prepare pnpm@latest-9 --activate \
Expand Down
33 changes: 33 additions & 0 deletions docs/data-sources/public_cloud_isos.md
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
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" {}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.25.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.11.0
github.com/leaseweb/leaseweb-go-sdk/v2 v2.0.2
github.com/leaseweb/leaseweb-go-sdk/v2 v2.0.4
github.com/stretchr/testify v1.10.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leaseweb/leaseweb-go-sdk/v2 v2.0.2 h1:3DrXQHsXe2tCbEcQNQoU7+7MXZADW7tYLcW710s2hFI=
github.com/leaseweb/leaseweb-go-sdk/v2 v2.0.2/go.mod h1:DkYJFyjpKDTDdSVDrGLzzMDxH8cTM3VbejpM3p4Yh0I=
github.com/leaseweb/leaseweb-go-sdk/v2 v2.0.4 h1:uyqvieR/5mdU5DoESMs1vr2rCZBOwzi2di74yr8F5PI=
github.com/leaseweb/leaseweb-go-sdk/v2 v2.0.4/go.mod h1:DkYJFyjpKDTDdSVDrGLzzMDxH8cTM3VbejpM3p4Yh0I=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (p *leasewebProvider) DataSources(_ context.Context) []func() datasource.Da
publiccloud.NewLoadBalancersDataSource,
publiccloud.NewLoadBalancerListenersDataSource,
publiccloud.NewTargetGroupsDataSource,
publiccloud.NewISOsDataSource,
}
}

Expand Down
31 changes: 31 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,37 @@ resource "leaseweb_dedicated_server" "test" {
})
}

func TestAccPublicCloudISOsDataSource(t *testing.T) {
t.Run("can read all ISOs", func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: providerConfig + `data "leaseweb_public_cloud_isos" "test" {}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(
"data.leaseweb_public_cloud_isos.test",
"isos.#",
"2",
),
resource.TestCheckResourceAttr(
"data.leaseweb_public_cloud_isos.test",
"isos.0.id",
"GRML",
),
resource.TestCheckResourceAttr(
"data.leaseweb_public_cloud_isos.test",
"isos.0.name",
"GRML 2022.11",
),
),
},
},
})
})
}

func TestAccPublicCloudIpResource(t *testing.T) {
t.Run("imports and updates an ip", func(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand Down
167 changes: 167 additions & 0 deletions internal/provider/publiccloud/isos_data_source.go
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",
}
}
31 changes: 31 additions & 0 deletions internal/provider/publiccloud/isos_data_source_test.go
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())
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"husky": "9.1.7",
"lint-staged": "15.2.10",
"markdownlint-cli": "0.43.0",
"prettier": "3.4.1",
"prettier": "3.4.2",
"prettier-plugin-sort-json": "4.0.0"
},
"lint-staged": {
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5f3aa0

Please sign in to comment.