Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add location datasource/resource #192

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/location.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Test location ds/rs

on:
workflow_dispatch:
push:
branches:
- master
paths:
- "**.go"
- ".github/workflows/location.yml"
pull_request:
branches:
- master
paths:
- "**gridscale_location**"
- "gridscale/location-utils/**"
- "gridscale/error-handler/**"
- "gridscale/common.go"
- "gridscale/config.go"
- "gridscale/provider.go"
- "gridscale/provider_test.go"
- ".github/workflows/location.yml"

jobs:
build:
name: Location AccTest
runs-on: ubuntu-latest
env:
GOPATH: /home/runner/go
GRIDSCALE_UUID: ${{ secrets.CI_USER_UUID }}
GRIDSCALE_TOKEN: ${{ secrets.CI_API_TOKEN }}
GRIDSCALE_URL: ${{ secrets.CI_API_URL }}
steps:
- name: Set up Go 1.16
uses: actions/setup-go@v2
with:
go-version: ^1.16
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Run TestAccdataSourceGridscaleLocation_basic
run: make testacc TEST=./gridscale TESTARGS='-run=TestAccdataSourceGridscaleLocation_basic'

- name: Run TestAccResourceGridscaleLocation_Basic
run: make testacc TEST=./gridscale TESTARGS='-run=TestAccResourceGridscaleLocation_Basic'
244 changes: 244 additions & 0 deletions gridscale/datasource_gridscale_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
package gridscale

import (
"context"
"fmt"

"github.com/gridscale/gsclient-go/v3"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceGridscaleLocation() *schema.Resource {
return &schema.Resource{
Read: dataSourceGridscaleLocationRead,

Schema: map[string]*schema.Schema{
"resource_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of a resource",
ValidateFunc: validation.NoZeroValues,
},
"name": {
Type: schema.TypeString,
Description: "The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters.",
Computed: true,
},
"labels": {
Type: schema.TypeSet,
Description: "List of labels.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"parent_location_uuid": {
Type: schema.TypeString,
Description: "The location_uuid of an existing public location in which to create the private location.",
Computed: true,
},
"cpunode_count": {
Type: schema.TypeInt,
Description: "The number of dedicated cpunodes to assigne to the private location.",
Computed: true,
},
"product_no": {
Type: schema.TypeInt,
Description: "The product number of a valid and available dedicated cpunode article.",
Computed: true,
},
"iata": {
Type: schema.TypeString,
Description: "IATA airport code, which works as a location identifier.",
Computed: true,
},
"country": {
Type: schema.TypeString,
Description: "The human-readable name of the location. It supports the full UTF-8 character set, with a maximum of 64 characters.",
Computed: true,
},
"active": {
Type: schema.TypeBool,
Description: "True if the location is active.",
Computed: true,
},
"cpunode_count_change_requested": {
Type: schema.TypeInt,
Description: "The requested number of dedicated cpunodes.",
Computed: true,
},
"product_no_change_requested": {
Type: schema.TypeInt,
Description: "The product number of a valid and available dedicated cpunode article.",
Computed: true,
},
"parent_location_uuid_change_requested": {
Type: schema.TypeString,
Description: "The location_uuid of an existing public location in which to create the private location.",
Computed: true,
},
"public": {
Type: schema.TypeBool,
Description: "True if this location is publicly available or a private location.",
Computed: true,
},
"certification_list": {
Type: schema.TypeString,
Description: "List of certifications.",
Computed: true,
},
"city": {
Type: schema.TypeString,
Description: "The human-readable name of the location. It supports the full UTF-8 character set, with a maximum of 64 characters.",
Computed: true,
},
"data_protection_agreement": {
Type: schema.TypeString,
Description: "Data protection agreement.",
Computed: true,
},
"geo_location": {
Type: schema.TypeString,
Description: "Geo location.",
Computed: true,
},
"green_energy": {
Type: schema.TypeString,
Description: "Green energy.",
Computed: true,
},
"operator_certification_list": {
Type: schema.TypeString,
Description: "List of operator certifications.",
Computed: true,
},
"owner": {
Type: schema.TypeString,
Description: "The human-readable name of the owner.",
Computed: true,
},
"owner_website": {
Type: schema.TypeString,
Description: "The website of the owner.",
Computed: true,
},
"site_name": {
Type: schema.TypeString,
Description: "The human-readable name of the website.",
Computed: true,
},
"hardware_profiles": {
Type: schema.TypeString,
Description: "List of supported hardware profiles.",
Computed: true,
},
"has_rocket_storage": {
Type: schema.TypeString,
Description: "TRUE if the location supports rocket storage.",
Computed: true,
},
"has_server_provisioning": {
Type: schema.TypeString,
Description: "TRUE if the location supports server provisioning.",
Computed: true,
},
"object_storage_region": {
Type: schema.TypeString,
Description: "The region of the object storage.",
Computed: true,
},
"backup_center_location_uuid": {
Type: schema.TypeString,
Description: "The location_uuid of a backup location.",
Computed: true,
},
},
}
}

func dataSourceGridscaleLocationRead(d *schema.ResourceData, meta interface{}) error {
id := d.Get("resource_id").(string)
errorPrefix := fmt.Sprintf("read location (%s) dataSource -", id)
client := meta.(*gsclient.Client)
loc, err := client.GetLocation(context.Background(), id)
if err != nil {
return fmt.Errorf("%s error: %v", errorPrefix, err)
}
locProp := loc.Properties
d.SetId(locProp.ObjectUUID)
if err = d.Set("name", locProp.Name); err != nil {
return fmt.Errorf("%s error setting name: %v", errorPrefix, err)
}
if err = d.Set("iata", locProp.Iata); err != nil {
return fmt.Errorf("%s error setting iata: %v", errorPrefix, err)
}
if err = d.Set("country", locProp.Country); err != nil {
return fmt.Errorf("%s error setting country: %v", errorPrefix, err)
}
if err = d.Set("active", locProp.Active); err != nil {
return fmt.Errorf("%s error setting active: %v", errorPrefix, err)
}
if err = d.Set("cpunode_count_change_requested", locProp.ChangeRequested.CPUNodeCount); err != nil {
return fmt.Errorf("%s error setting cpunode_count_change_requested: %v", errorPrefix, err)
}
if err = d.Set("product_no_change_requested", locProp.ChangeRequested.ProductNo); err != nil {
return fmt.Errorf("%s error setting product_no_change_requested: %v", errorPrefix, err)
}
if err = d.Set("parent_location_uuid_change_requested", locProp.ChangeRequested.ParentLocationUUID); err != nil {
return fmt.Errorf("%s error setting parent_location_uuid_change_requested: %v", errorPrefix, err)
}
if err = d.Set("cpunode_count", locProp.CPUNodeCount); err != nil {
return fmt.Errorf("%s error setting cpunode_count: %v", errorPrefix, err)
}
if err = d.Set("public", locProp.Public); err != nil {
return fmt.Errorf("%s error setting public: %v", errorPrefix, err)
}
if err = d.Set("product_no", locProp.ProductNo); err != nil {
return fmt.Errorf("%s error setting product_no: %v", errorPrefix, err)
}
if err = d.Set("certification_list", locProp.LocationInformation.CertificationList); err != nil {
return fmt.Errorf("%s error setting certification_list: %v", errorPrefix, err)
}
if err = d.Set("city", locProp.LocationInformation.City); err != nil {
return fmt.Errorf("%s error setting city: %v", errorPrefix, err)
}
if err = d.Set("data_protection_agreement", locProp.LocationInformation.DataProtectionAgreement); err != nil {
return fmt.Errorf("%s error setting data_protection_agreement: %v", errorPrefix, err)
}
if err = d.Set("geo_location", locProp.LocationInformation.GeoLocation); err != nil {
return fmt.Errorf("%s error setting geo_location: %v", errorPrefix, err)
}
if err = d.Set("green_energy", locProp.LocationInformation.GreenEnergy); err != nil {
return fmt.Errorf("%s error setting green_energy: %v", errorPrefix, err)
}
if err = d.Set("operator_certification_list", locProp.LocationInformation.OperatorCertificationList); err != nil {
return fmt.Errorf("%s error setting operator_certification_list: %v", errorPrefix, err)
}
if err = d.Set("owner", locProp.LocationInformation.Owner); err != nil {
return fmt.Errorf("%s error setting owner: %v", errorPrefix, err)
}
if err = d.Set("owner_website", locProp.LocationInformation.OwnerWebsite); err != nil {
return fmt.Errorf("%s error setting owner_website: %v", errorPrefix, err)
}
if err = d.Set("site_name", locProp.LocationInformation.SiteName); err != nil {
return fmt.Errorf("%s error setting site_name: %v", errorPrefix, err)
}
if err = d.Set("hardware_profiles", locProp.Features.HardwareProfiles); err != nil {
return fmt.Errorf("%s error setting hardware_profiles: %v", errorPrefix, err)
}
if err = d.Set("has_rocket_storage", locProp.Features.HasRocketStorage); err != nil {
return fmt.Errorf("%s error setting has_rocket_storage: %v", errorPrefix, err)
}
if err = d.Set("has_server_provisioning", locProp.Features.HasServerProvisioning); err != nil {
return fmt.Errorf("%s error setting has_server_provisioning: %v", errorPrefix, err)
}
if err = d.Set("object_storage_region", locProp.Features.ObjectStorageRegion); err != nil {
return fmt.Errorf("%s error setting object_storage_region: %v", errorPrefix, err)
}
if err = d.Set("backup_center_location_uuid", locProp.Features.BackupCenterLocationUUID); err != nil {
return fmt.Errorf("%s error setting backup_center_location_uuid: %v", errorPrefix, err)
}
if err = d.Set("labels", locProp.Labels); err != nil {
return fmt.Errorf("%s error setting labels: %v", errorPrefix, err)
}
return nil
}
36 changes: 36 additions & 0 deletions gridscale/datasource_gridscale_location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gridscale

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccdataSourceGridscaleLocation_basic(t *testing.T) {
locUUID := "45ed677b-3702-4b36-be2a-a2eab9827950"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{

Config: testAccCheckDataSourceLocationConfig_basic(locUUID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.gridscale_location.foo", "id", locUUID),
resource.TestCheckResourceAttrSet("data.gridscale_location.foo", "name"),
resource.TestCheckResourceAttrSet("data.gridscale_location.foo", "cpunode_count"),
),
},
},
})

}

func testAccCheckDataSourceLocationConfig_basic(locUUID string) string {
return fmt.Sprintf(`
data "gridscale_location" "foo" {
resource_id = "%s"
}`, locUUID)
}
2 changes: 2 additions & 0 deletions gridscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Provider() *schema.Provider {
"gridscale_firewall": dataSourceGridscaleFirewall(),
"gridscale_marketplace_application": dataSourceGridscaleMarketplaceApplication(),
"gridscale_ssl_certificate": dataSourceGridscaleSSLCert(),
"gridscale_location": dataSourceGridscaleLocation(),
},
ResourcesMap: map[string]*schema.Resource{
"gridscale_server": resourceGridscaleServer(),
Expand Down Expand Up @@ -106,6 +107,7 @@ func Provider() *schema.Provider {
"gridscale_marketplace_application": resourceGridscaleMarketplaceApplication(),
"gridscale_marketplace_application_import": resourceGridscaleImportedMarketplaceApplication(),
"gridscale_ssl_certificate": resourceGridscaleSSLCert(),
"gridscale_location": resourceGridscaleLocation(),
},

ConfigureFunc: providerConfigure,
Expand Down