Skip to content

Commit

Permalink
Merge pull request #4 from PNAP/master
Browse files Browse the repository at this point in the history
Added reservation resource
  • Loading branch information
pajuga authored Feb 7, 2022
2 parents d2f7de5 + c1a9409 commit 8271fca
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ builds:
ignore:
- goos: darwin
goarch: '386'
- goos: darwin
goarch: amd64
#- goos: darwin
# goarch: amd64
- goos: freebsd
goarch: arm64
binary: '{{ .ProjectName }}_v{{ .Version }}'
Expand All @@ -45,6 +45,8 @@ signs:
# if you are using this is a GitHub action or some other automated pipeline, you
# need to pass the batch flag to indicate its not interactive.
- "--batch"
- "--no-tty"
- "--status-fd=2"
- "--local-user"
- "{{ .Env.GPG_FINGERPRINT }}" # set this environment variable for your signing key
- "--output"
Expand Down
58 changes: 58 additions & 0 deletions docs/data-sources/reservation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
layout: "pnap"
page_title: "phoenixNAP: pnap_reservation"
sidebar_current: "docs-pnap-datasource-reservation"
description: |-
Provides a phoenixNAP reservation datasource. This can be used to read reservation details.
---

# pnap_reservation Datasource

Provides a phoenixNAP reservation datasource. This can be used to read reservation details.



## Example Usage

Fetch a reservation by ID or SKU and show it's details in alphabetical order.

```hcl
# Fetch a reservation
data "pnap_reservation" "test" {
id = "e6afba51-7de8-4080-83ab-0f915570659c"
sku = "XXX-XXX-XXX"
}
# Show the reservation details
output "reservation" {
value = data.pnap_reservation.test
}
```

## Argument Reference

The following arguments are supported:

* `id` - The reservation identifier.
* `sku` - The SKU code of product pricing plan.


## Attributes Reference

The following attributes are exported:

* `id` - The reservation identifier.
* `product_code` - The code identifying the product. This code has significance across all locations.
* `product_category` - The product category.
* `location` - The location code.
* `reservation_model` - The reservation model.
* `initial_invoice_model` - Reservations created with initial invoice model ON_CREATION will be invoiced on same date when reservation is created. Reservation created with CALENDAR_MONTH initial invoice model will be invoiced at the begining of next month.
* `start_date_time` - The point in time (in UTC) when the reservation starts.
* `end_date_time` - The point in time (in UTC) when the reservation ends.
* `last_renewal_date_time` - The point in time (in UTC) when the reservation was renewed last.
* `next_renewal_date_time` - The point in time (in UTC) when the reservation will be renewed if auto renew is set to true.
* `auto_renew` - A flag indicating whether the reservation will auto-renew (default is true, it can only be modified after the creation of resource).
* `sku` - The SKU applied to this reservation.
* `price` - Reservation price.
* `price_unit` - The unit to which the price applies.
* `assigned_resource_id` - The resource ID currently being assigned to reservation.
54 changes: 54 additions & 0 deletions docs/resources/reservation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
layout: "pnap"
page_title: "phoenixNAP: pnap_reservation"
sidebar_current: "docs-pnap-resource-reservation"
description: |-
Provides a phoenixNAP reservation resource. This can be used to create and modify reservations.
---

# pnap_reservation Resource

Provides a phoenixNAP reservation resource. This can be used to create and modify reservations.



## Example Usage

Create a reservation

```hcl
# Create a reservation
resource "pnap_reservation" "Test-Reservation-1" {
sku = "XXX-XXX-XXX"
}
```

## Argument Reference

The following arguments are supported:

* `sku` - (Required) The SKU code of product pricing plan.
* `auto_renew` - A flag indicating whether the reservation will auto-renew (default is true, it can only be modified after the creation of resource).
* `auto_renew_disable_reason` - The reason for disabling auto-renewal.


## Attributes Reference

The following attributes are exported:

* `id` - The reservation identifier.
* `product_code` - The code identifying the product. This code has significance across all locations.
* `product_category` - The product category.
* `location` - The location code.
* `reservation_model` - The reservation model.
* `initial_invoice_model` - Reservations created with initial invoice model ON_CREATION will be invoiced on same date when reservation is created. Reservation created with CALENDAR_MONTH initial invoice model will be invoiced at the begining of next month.
* `start_date_time` - The point in time (in UTC) when the reservation starts.
* `end_date_time` - The point in time (in UTC) when the reservation ends.
* `last_renewal_date_time` - The point in time (in UTC) when the reservation was renewed last.
* `next_renewal_date_time` - The point in time (in UTC) when the reservation will be renewed if auto renew is set to true.
* `auto_renew` - A flag indicating whether the reservation will auto-renew (default is true, it can only be modified after the creation of resource).
* `sku` - The SKU that will be applied to this reservation.
* `price` - Reservation price.
* `price_unit` - The unit to which the price applies.
* `assigned_resource_id` - The resource ID currently being assigned to reservation.

169 changes: 169 additions & 0 deletions pnap/data_source_reservation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package pnap

import (
"fmt"

"github.com/PNAP/go-sdk-helper-bmc/command/billingapi/reservation"
"github.com/PNAP/go-sdk-helper-bmc/receiver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceReservation() *schema.Resource {
return &schema.Resource{
Read: dataSourceReservationRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"product_code": {
Type: schema.TypeString,
Computed: true,
},
"product_category": {
Type: schema.TypeString,
Computed: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},
"reservation_model": {
Type: schema.TypeString,
Computed: true,
},
"initial_invoice_model": {
Type: schema.TypeString,
Computed: true,
},
"start_date_time": {
Type: schema.TypeString,
Computed: true,
},
"end_date_time": {
Type: schema.TypeString,
Computed: true,
},
"last_renewal_date_time": {
Type: schema.TypeString,
Computed: true,
},
"next_renewal_date_time": {
Type: schema.TypeString,
Computed: true,
},
"auto_renew": {
Type: schema.TypeBool,
Computed: true,
},
"sku": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"price": {
Type: schema.TypeFloat,
Computed: true,
},
"price_unit": {
Type: schema.TypeString,
Computed: true,
},
"assigned_resource_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceReservationRead(d *schema.ResourceData, m interface{}) error {
client := m.(receiver.BMCSDK)
requestCommand := reservation.NewGetReservationsCommand(client)
resp, err := requestCommand.Execute()
if err != nil {
return err
}
if len(d.Get("id").(string)) > 0 && len(d.Get("sku").(string)) > 0 {
numOfKeys := 0
for _, instance := range resp {
if instance.ID == d.Get("id").(string) && instance.SKU == d.Get("sku").(string) {
numOfKeys++
d.SetId(instance.ID)
d.Set("id", instance.ID)
d.Set("product_code", instance.ProductCode)
d.Set("product_category", instance.ProductCategory)
d.Set("location", instance.Location)
d.Set("reservation_model", instance.ReservationModel)
d.Set("initial_invoice_model", instance.InitialInvoiceModel)
d.Set("start_date_time", instance.StartDateTime.String())
d.Set("end_date_time", instance.EndDateTime.String())
d.Set("last_renewal_date_time", instance.LastRenewalDateTime.String())
d.Set("next_renewal_date_time", instance.NextRenewalDateTime.String())
d.Set("auto_renew", instance.AutoRenew)
d.Set("sku", instance.SKU)
d.Set("price", instance.Price)
d.Set("price_unit", instance.PriceUnit)
d.Set("assigned_resource_id", instance.AssignedResourceID)
}
}
if numOfKeys > 1 {
return fmt.Errorf("too many reservations with id %s and sku %s (found %d, expected 1)", d.Get("id").(string), d.Get("sku").(string), numOfKeys)
}
} else if len(d.Get("sku").(string)) > 0 {
numOfKeys := 0
for _, instance := range resp {
if instance.SKU == d.Get("sku").(string) {
numOfKeys++
d.SetId(instance.ID)
d.Set("id", instance.ID)
d.Set("product_code", instance.ProductCode)
d.Set("product_category", instance.ProductCategory)
d.Set("location", instance.Location)
d.Set("reservation_model", instance.ReservationModel)
d.Set("initial_invoice_model", instance.InitialInvoiceModel)
d.Set("start_date_time", instance.StartDateTime.String())
d.Set("end_date_time", instance.EndDateTime.String())
d.Set("last_renewal_date_time", instance.LastRenewalDateTime.String())
d.Set("next_renewal_date_time", instance.NextRenewalDateTime.String())
d.Set("auto_renew", instance.AutoRenew)
d.Set("sku", instance.SKU)
d.Set("price", instance.Price)
d.Set("price_unit", instance.PriceUnit)
d.Set("assigned_resource_id", instance.AssignedResourceID)
}
}
if numOfKeys > 1 {
return fmt.Errorf("too many reservations with sku %s (found %d, expected 1)", d.Get("sku").(string), numOfKeys)
}
} else {
numOfKeys := 0
for _, instance := range resp {
if instance.ID == d.Get("id").(string) {
numOfKeys++
d.SetId(instance.ID)
d.Set("id", instance.ID)
d.Set("product_code", instance.ProductCode)
d.Set("product_category", instance.ProductCategory)
d.Set("location", instance.Location)
d.Set("reservation_model", instance.ReservationModel)
d.Set("initial_invoice_model", instance.InitialInvoiceModel)
d.Set("start_date_time", instance.StartDateTime.String())
d.Set("end_date_time", instance.EndDateTime.String())
d.Set("last_renewal_date_time", instance.LastRenewalDateTime.String())
d.Set("next_renewal_date_time", instance.NextRenewalDateTime.String())
d.Set("auto_renew", instance.AutoRenew)
d.Set("sku", instance.SKU)
d.Set("price", instance.Price)
d.Set("price_unit", instance.PriceUnit)
d.Set("assigned_resource_id", instance.AssignedResourceID)
}
}
if numOfKeys > 1 {
return fmt.Errorf("too many reservations with id %s (found %d, expected 1)", d.Get("id").(string), numOfKeys)
}
}
return nil
}
2 changes: 2 additions & 0 deletions pnap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ func Provider() *schema.Provider {
"pnap_ssh_key": resourceSshKey(),
"pnap_server": resourceServer(),
"pnap_private_network": resourcePrivateNetwork(),
"pnap_reservation": resourceReservation(),
},
DataSourcesMap: map[string]*schema.Resource{
"pnap_ssh_key": dataSourceSshKey(),
"pnap_server": dataSourceServer(),
"pnap_private_network": dataSourcePrivateNetwork(),
"pnap_reservation": dataSourceReservation(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
Loading

0 comments on commit 8271fca

Please sign in to comment.