Skip to content

Commit

Permalink
Included support for GLP IAM v1 (#197)
Browse files Browse the repository at this point in the history
* Included changes for GLP IAM V1

* Improved docs

* Updated metal client to v1.5.13

* Fixed formatting

* Implemented review comments and fixed lint

* Updated version
  • Loading branch information
dbozzato81 authored May 28, 2024
1 parent 1e56d1c commit aeef0f9
Show file tree
Hide file tree
Showing 21 changed files with 156 additions and 75 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ $ make build
Note: For debugging the provider please refer to the
[debugging guide](https://medium.com/@gandharva666/debugging-terraform-using-jetbrains-goland-f9a7e992cb1d)

## Using GreenLake tokens
## Using GreenLake Cloud Services (GLCS) tokens

**NOTE**: The below steps are applicable only when using stand-alone provider. If you are using [hpegl provider](https://registry.terraform.io/providers/HPE/hpegl/latest/docs),
then follow the steps explained on that page to specify the parameters.

When using GreenLake tokens, the required parameters is to be provided in a `.gltform` file.
When using GLCS tokens, the required parameters is to be provided in a `.gltform` file.
This file can be written in home or in the directory from which terraform is run.

The file contents:
Expand All @@ -59,6 +59,22 @@ access_token: <...>
`space_name` is optional, and is only required if the terraform provider is going to be used to create projects.
Access token may be obtained by logging into HPE GreenLake Central and then clicking **API Access** on the User menu.

## Using GreenLake Platform (GLP) tokens

**NOTE**: The below steps are applicable only when using stand-alone provider. If you are using [hpegl provider](https://registry.terraform.io/providers/HPE/hpegl/latest/docs),
then follow the steps explained on that page to specify the parameters.

When using GreenLake tokens, the required parameters is to be provided in a `.gltform` file.
This file can be written in home or in the directory from which terraform is run.

The file contents:

```yaml
rest_url: http://localhost:3002
project_id: 65c82181-fefc-4ea7-870e-628225fe7664
access_token: <...>
```


## Using Metal tokens

Expand Down
13 changes: 12 additions & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ terraform {
}
}

# Example of provider configuration when using GreenLake IAM token
# Example of provider configuration when using GreenLake Cloud Services (GLCS) IAM token
provider "hpegl" {
metal {
rest_url = "https://localhost:3002"
space_name = "space_name"
project_id = "1d96bfbc-9cf0-4268-aac6-ca1c65aca385"

}
}

# Example of provider configuration when using GreenLake Platform (GLP) IAM token
provider "hpegl" {
metal {
rest_url = "https://localhost:3002"
project_id = "1d96bfbc-9cf0-4268-aac6-ca1c65aca385"
glp_workspace = "1a2ba81600dd11efa47076a3447ec4eb"
glp_role = "service-platform-owner"
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/golangci/golangci-lint v1.58.2
github.com/hashicorp/terraform-plugin-docs v0.19.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/hewlettpackard/hpegl-metal-client v1.5.11
github.com/hewlettpackard/hpegl-metal-client v1.5.13
github.com/hewlettpackard/hpegl-provider-lib v0.0.15
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/hewlettpackard/hpegl-metal-client v1.5.11 h1:uTCGYRrKGG/Nd5i7KLL9jSW1XDYrB1pKI3mpZsKtSYM=
github.com/hewlettpackard/hpegl-metal-client v1.5.11/go.mod h1:E72/o32a5WwVAUhXvXEUKZOGiSA84hsTS+xezFqtbgU=
github.com/hewlettpackard/hpegl-metal-client v1.5.13 h1:c4Gb9Gu449LmN0eoDVTwGgkg/rAEaiDQ8jEfMzTXtWU=
github.com/hewlettpackard/hpegl-metal-client v1.5.13/go.mod h1:E72/o32a5WwVAUhXvXEUKZOGiSA84hsTS+xezFqtbgU=
github.com/hewlettpackard/hpegl-provider-lib v0.0.15 h1:yDqJNUYDq37LjpwoqvNKv2RS2s5gCEL+qQ3MD0WXIdw=
github.com/hewlettpackard/hpegl-provider-lib v0.0.15/go.mod h1:9lEPIb9rKqxZNPkwcgRN+3/rAkpyXCk3KR8GFO5xaZ4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand Down
6 changes: 3 additions & 3 deletions internal/acceptance_test/resource_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func testWaitUntilHostReady(rsrc string) resource.TestCheckFunc {
for i := 0; i < hostStatePollCount && hostState != rest.HOSTSTATE_READY; i++ {
time.Sleep(hostStateReadyWait)

host, resp, err := p.Client.HostsApi.GetByID(ctx, hostID)
host, resp, err := p.Client.HostsApi.GetByID(ctx, hostID, nil)
if err != nil {
return fmt.Errorf("Host: %q not found: %s", hostID, err)
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func testVerifyHostReady(resourceStateKey string) resource.TestCheckFunc {

ctx := p.GetContext()

host, resp, err := p.Client.HostsApi.GetByID(ctx, hostID)
host, resp, err := p.Client.HostsApi.GetByID(ctx, hostID, nil)
if err != nil {
return fmt.Errorf("Host: %q not found: %s", hostID, err)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func testAccCheckHostDestroy(t *testing.T, s *terraform.State) error {

ctx := p.GetContext()

_, resp, err := p.Client.HostsApi.GetByID(ctx, hostID)
_, resp, err := p.Client.HostsApi.GetByID(ctx, hostID, nil)
if err != nil {
return fmt.Errorf("Error retrieving host %s: %v", hostID, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance_test/resource_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func testAccCheckImageDestroy(t *testing.T, s *terraform.State) error {

ctx := p.GetContext()

_, res, err := p.Client.ServicesApi.GetByID(ctx, imageID)
_, res, err := p.Client.ServicesApi.GetByID(ctx, imageID, nil)
if err == nil {
return fmt.Errorf("image %v still exists", imageID)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func testAccCheckImageExists(resource string) resource.TestCheckFunc {

ctx := p.GetContext()

ret, res, err := p.Client.ServicesApi.GetByID(ctx, imageID)
ret, res, err := p.Client.ServicesApi.GetByID(ctx, imageID, nil)
if err != nil {
return fmt.Errorf("image %v not found: %s", imageID, err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/acceptance_test/resource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func testAccCheckNetworkDestroy(t *testing.T, s *terraform.State) error {

ctx := p.GetContext()

_, _, err := p.Client.NetworksApi.GetByID(ctx, rs.Primary.ID)
//nolint:bodyclose // Response body is closed by metal client.
_, _, err := p.Client.NetworksApi.GetByID(ctx, rs.Primary.ID, nil)
if err == nil {
return fmt.Errorf("Alert pnet still exists")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance_test/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func testAccCheckProjectDestroy(t *testing.T, s *terraform.State) error {

ctx := p.GetContext()

_, res, err := p.Client.ProjectsApi.GetByID(ctx, ProjectID)
_, res, err := p.Client.ProjectsApi.GetByID(ctx, ProjectID, nil)
if err == nil {
return fmt.Errorf("Project %v still exists", ProjectID)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func testAccCheckProjectExists(resource string) resource.TestCheckFunc {

ctx := p.GetContext()

ret, res, err := p.Client.ProjectsApi.GetByID(ctx, ProjectID)
ret, res, err := p.Client.ProjectsApi.GetByID(ctx, ProjectID, nil)
if err != nil {
return fmt.Errorf("Project %v not found: %s", ProjectID, err)
}
Expand Down
7 changes: 5 additions & 2 deletions internal/acceptance_test/resource_ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func testAccCheckSSHKeyExists(n string, out *rest.SshKey) resource.TestCheckFunc

ctx := p.GetContext()

key, _, err := p.Client.SshkeysApi.GetByID(ctx, rs.Primary.ID)
//nolint:bodyclose // Response body is closed by metal client.
key, _, err := p.Client.SshkeysApi.GetByID(ctx, rs.Primary.ID, nil)
if err != nil {
return err
}
Expand All @@ -99,7 +100,9 @@ func testAccCheckSSHKeyDestroy(s *terraform.State) error {
}

ctx := p.GetContext()
if _, _, err := p.Client.SshkeysApi.GetByID(ctx, rs.Primary.ID); err == nil {

//nolint:bodyclose // Response body is closed by metal client.
if _, _, err := p.Client.SshkeysApi.GetByID(ctx, rs.Primary.ID, nil); err == nil {
return fmt.Errorf("SSHKey still exists")
}
}
Expand Down
6 changes: 4 additions & 2 deletions internal/acceptance_test/resource_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func testAccCheckVolumeDestroy(t *testing.T, s *terraform.State) error {

ctx := p.GetContext()

volume, _, err := p.Client.VolumesApi.GetByID(ctx, volumeID)
//nolint:bodyclose // Response body is closed by metal client.
volume, _, err := p.Client.VolumesApi.GetByID(ctx, volumeID, nil)
if err == nil && volume.State != rest.VOLUMESTATE_DELETED {
return fmt.Errorf("Volume: %v still exists", volume)
}
Expand Down Expand Up @@ -142,7 +143,8 @@ func testAccCheckVolumeExists(resource string, id *string) resource.TestCheckFun

ctx := p.GetContext()

_, _, err = p.Client.VolumesApi.GetByID(ctx, volumeID)
//nolint:bodyclose // Response body is closed by metal client.
_, _, err = p.Client.VolumesApi.GetByID(ctx, volumeID, nil)
if err != nil {
return fmt.Errorf("Volume: %q not found: %s", volumeID, err)
}
Expand Down
33 changes: 17 additions & 16 deletions internal/resources/resource_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func resourceMetalHostCreate(d *schema.ResourceData, meta interface{}) (err erro
// Create it
ctx := p.GetContext()

h, _, err := p.Client.HostsApi.Add(ctx, host)
h, _, err := p.Client.HostsApi.Add(ctx, host, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -516,7 +516,7 @@ func resourceMetalHostCreate(d *schema.ResourceData, meta interface{}) (err erro
string(rest.HOSTSTATE_READY),
},
Refresh: func() (interface{}, string, error) {
host, _, err := p.Client.HostsApi.GetByID(ctx, h.ID)
host, _, err := p.Client.HostsApi.GetByID(ctx, h.ID, nil)
if err != nil {
return nil, "", fmt.Errorf("get host %v", h.ID)
}
Expand Down Expand Up @@ -545,7 +545,7 @@ func resourceMetalHostRead(d *schema.ResourceData, meta interface{}) (err error)
}

ctx := p.GetContext()
host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id())
host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id(), nil)
if err != nil {
return err
}
Expand All @@ -569,7 +569,7 @@ func resourceMetalHostRead(d *schema.ResourceData, meta interface{}) (err error)
return fmt.Errorf("set summary status: %v", err)
}

varesources, _, err := p.Client.VolumeAttachmentsApi.List(ctx)
varesources, _, err := p.Client.VolumeAttachmentsApi.List(ctx, nil)
if err != nil {
return fmt.Errorf("error reading volume attachment information %v", err)
}
Expand Down Expand Up @@ -690,16 +690,17 @@ func resourceMetalHostUpdate(d *schema.ResourceData, meta interface{}) (err erro

ctx := p.GetContext()

host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id())
host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id(), nil)
if err != nil {
return err
}

volumes, _, err := p.Client.VolumesApi.List(ctx)
volumes, _, err := p.Client.VolumesApi.List(ctx, nil)
if err != nil {
return fmt.Errorf("error reading volume information %v", err)
}
varesources, _, err := p.Client.VolumeAttachmentsApi.List(ctx)

varesources, _, err := p.Client.VolumeAttachmentsApi.List(ctx, nil)
if err != nil {
return fmt.Errorf("error reading volume attachment information %v", err)
}
Expand Down Expand Up @@ -730,7 +731,7 @@ func resourceMetalHostUpdate(d *schema.ResourceData, meta interface{}) (err erro
// detach
vaHostID := rest.VolumeAttachHostUuid{HostID: host.ID}
for _, dv := range detachList {
_, err = p.Client.VolumesApi.Detach(ctx, dv, vaHostID)
_, err = p.Client.VolumesApi.Detach(ctx, dv, vaHostID, nil)

if err != nil {
return err
Expand All @@ -739,7 +740,7 @@ func resourceMetalHostUpdate(d *schema.ResourceData, meta interface{}) (err erro

// attach
for _, av := range attachList {
_, _, err = p.Client.VolumesApi.Attach(ctx, av, vaHostID)
_, _, err = p.Client.VolumesApi.Attach(ctx, av, vaHostID, nil)

if err != nil {
return err
Expand Down Expand Up @@ -790,7 +791,7 @@ func resourceMetalHostUpdate(d *schema.ResourceData, meta interface{}) (err erro
// Update.
ctx = p.GetContext()

_, _, err = p.Client.HostsApi.Update(ctx, updateHost.ID, updateHost)
_, _, err = p.Client.HostsApi.Update(ctx, updateHost.ID, updateHost, nil)
if err != nil {
//nolint:wrapcheck // defer func is wrapping the error.
return err
Expand All @@ -816,7 +817,7 @@ func resourceMetalHostUpdate(d *schema.ResourceData, meta interface{}) (err erro
string(rest.HOSTSTATE_READY),
},
Refresh: func() (interface{}, string, error) {
h, _, err := p.Client.HostsApi.GetByID(ctx, host.ID)
h, _, err := p.Client.HostsApi.GetByID(ctx, host.ID, nil)
if err != nil {
return nil, "", fmt.Errorf("get host %v", host.ID)
}
Expand Down Expand Up @@ -856,7 +857,7 @@ func resourceMetalHostDelete(d *schema.ResourceData, meta interface{}) (err erro

ctx := p.GetContext()

host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id())
host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id(), nil)
if err != nil {
return err
}
Expand All @@ -873,7 +874,7 @@ func resourceMetalHostDelete(d *schema.ResourceData, meta interface{}) (err erro
}
}

if _, err := p.Client.HostsApi.Delete(ctx, d.Id()); err != nil {
if _, err := p.Client.HostsApi.Delete(ctx, d.Id(), nil); err != nil {
//nolint:wrapcheck // defer func is wrapping the error.
return err
}
Expand All @@ -890,7 +891,7 @@ func resourceMetalHostDelete(d *schema.ResourceData, meta interface{}) (err erro
string(rest.HOSTSTATE_DELETED),
},
Refresh: func() (interface{}, string, error) {
host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id())
host, _, err := p.Client.HostsApi.GetByID(ctx, d.Id(), nil)
if err != nil {
return nil, "", fmt.Errorf("get host %v", d.Id())
}
Expand All @@ -910,7 +911,7 @@ func resourceMetalHostDelete(d *schema.ResourceData, meta interface{}) (err erro
}

func powerOffHost(ctx context.Context, hostAPI rest.HostsAPI, hostID string, timeout time.Duration) error {
_, _, err := hostAPI.PowerOff(ctx, hostID)
_, _, err := hostAPI.PowerOff(ctx, hostID, nil)
if err != nil {
return fmt.Errorf("power off host %v: %v", hostID, err)
}
Expand All @@ -925,7 +926,7 @@ func powerOffHost(ctx context.Context, hostAPI rest.HostsAPI, hostID string, tim
string(rest.HOSTPOWERSTATE_OFF),
},
Refresh: func() (interface{}, string, error) {
host, _, err := hostAPI.GetByID(ctx, hostID)
host, _, err := hostAPI.GetByID(ctx, hostID, nil)
if err != nil {
return nil, "", fmt.Errorf("get host %v", hostID)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/resource_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func resourceMetalImageDelete(d *schema.ResourceData, meta interface{}) (err err

ctx := p.GetContext()

if _, err = p.Client.ServicesApi.Delete(ctx, d.Id()); err != nil {
if _, err = p.Client.ServicesApi.Delete(ctx, d.Id(), nil); err != nil {
return err //nolint:wrapcheck // defer func is wrapping the error.
}

Expand All @@ -104,7 +104,7 @@ func resourceMetalImageUpdate(d *schema.ResourceData, meta interface{}) (err err

ctx := p.GetContext()

if _, _, err := p.Client.ServicesApi.Update(ctx, d.Id(), file); err != nil {
if _, _, err := p.Client.ServicesApi.Update(ctx, d.Id(), file, nil); err != nil {
return err //nolint:wrapcheck // defer func is wrapping the error.
}

Expand Down
8 changes: 4 additions & 4 deletions internal/resources/resource_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func resourceIPCreate(d *schema.ResourceData, meta interface{}) (err error) {

ctx := p.GetContext()

ipPools, _, err := p.Client.IppoolsApi.List(ctx)
ipPools, _, err := p.Client.IppoolsApi.List(ctx, nil)
if err != nil {
return err
}
Expand All @@ -83,7 +83,7 @@ func resourceIPCreate(d *schema.ResourceData, meta interface{}) (err error) {
}
}

if _, _, err := p.Client.IppoolsApi.AllocateIPs(ctx, poolID, []rest.IpAllocation{allocation}); err != nil {
if _, _, err := p.Client.IppoolsApi.AllocateIPs(ctx, poolID, []rest.IpAllocation{allocation}, nil); err != nil {
return err
}

Expand All @@ -104,7 +104,7 @@ func resourceIPRead(d *schema.ResourceData, meta interface{}) (err error) {
poolID := extractIPPoolID(d.Id())
allocIP := extractIP(d.Id())

ippool, _, err := p.Client.IppoolsApi.GetByID(ctx, poolID)
ippool, _, err := p.Client.IppoolsApi.GetByID(ctx, poolID, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func resourceIPDelete(d *schema.ResourceData, meta interface{}) (err error) {
poolID := extractIPPoolID(d.Id())
ip := extractIP(d.Id())

if _, _, err = p.Client.IppoolsApi.ReturnIPs(ctx, poolID, []string{ip}); err != nil {
if _, _, err = p.Client.IppoolsApi.ReturnIPs(ctx, poolID, []string{ip}, nil); err != nil {
return err
}

Expand Down
Loading

0 comments on commit aeef0f9

Please sign in to comment.