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

Implement resource glesys_privatenetwork glesys_privatenetwork_segment #204

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- `glesys_server` - `networkadapter` attribute listing attached NICS.
- `glesys_networkadapter` - Added Importer function.
- `glesys_privatenetwork` - Manage PrivateNetwork resources.
- `glesys_privatenetwork_segment` - Manage PrivateNetwork segments.

## [0.11.3] - 2024-06-10
### Changed
Expand Down
26 changes: 26 additions & 0 deletions docs/resources/privatenetwork.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
page_title: "glesys_privatenetwork Resource - terraform-provider-glesys"
subcategory: ""
description: |-
Create a PrivateNetwork resource.
---
# glesys_privatenetwork (Resource)
Create a PrivateNetwork resource.
## Example Usage
```terraform
resource "glesys_privatenetwork" "test" {
name = "mynet"
}
```
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) PrivateNetwork name

### Read-Only

- `id` (String) The ID of this resource.
- `ipv6aggregate` (String) IPv6Aggregate for the PrivateNetwork.

68 changes: 68 additions & 0 deletions docs/resources/privatenetwork_segment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
page_title: "glesys_privatenetwork_segment Resource - terraform-provider-glesys"
subcategory: ""
description: |-
Create a PrivateNetwork Segment to connect VM NetworkAdapters.
---
# glesys_privatenetwork_segment (Resource)
Create a PrivateNetwork Segment to connect VM NetworkAdapters.
## Example Usage
```terraform
### Basic segment

resource "glesys_privatenetwork_segment" "testseg" {
privatenetworkid = "pn-123ab"
datacenter = "dc-fbg1"
ipv4subnet = "10.0.0.0/24"
name = "seg-1"
platform = "kvm"
}

### PrivateNetwork, Segment And NetworkAdapter Example

resource "glesys_privatenetwork" "privatenet" {
name = "vm-internal"
}

// Segment attached to 'privatenet'
resource "glesys_privatenetwork_segment" "seg-kvm-fbg" {
privatenetworkid = glesys_privatenetwork.privatenet.id

name = "int-fbg"
datacenter = "dc-fbg1"
ipv4subnet = "10.2.0.0/24"
platform = "kvm"
}

resource "glesys_server" "myvm" {
platform = "KVM"
datacenter = "Falkenberg"
# ...
}

// NetworkAdapter attached to KVM vm 'myvm'
resource "glesys_networkadapter" "kvm-nic2" {
serverid = glesys_server.myvm.id

name = "pn-internal"
bandwidth = 1000
networkid = glesys_privatenetwork_segment.seg-kvm-fbg.id

}
```
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `datacenter` (String) PrivateNetworkSegment Datacenter.
- `ipv4subnet` (String) PrivateNetworkSegment IPv4 Subnet.
- `name` (String) PrivateNetworkSegment name
- `platform` (String) PrivateNetworkSegment Platform.
- `privatenetworkid` (String) PrivateNetwork ID for the Segment.

### Read-Only

- `id` (String) The ID of this resource.
- `ipv6subnet` (String) PrivateNetworkSegment IPv6 Subnet.

3 changes: 3 additions & 0 deletions examples/resources/glesys_privatenetwork/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "glesys_privatenetwork" "test" {
name = "mynet"
}
41 changes: 41 additions & 0 deletions examples/resources/glesys_privatenetwork_segment/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
### Basic segment

resource "glesys_privatenetwork_segment" "testseg" {
privatenetworkid = "pn-123ab"
datacenter = "dc-fbg1"
ipv4subnet = "10.0.0.0/24"
name = "seg-1"
platform = "kvm"
}

### PrivateNetwork, Segment And NetworkAdapter Example

resource "glesys_privatenetwork" "privatenet" {
name = "vm-internal"
}

// Segment attached to 'privatenet'
resource "glesys_privatenetwork_segment" "seg-kvm-fbg" {
privatenetworkid = glesys_privatenetwork.privatenet.id

name = "int-fbg"
datacenter = "dc-fbg1"
ipv4subnet = "10.2.0.0/24"
platform = "kvm"
}

resource "glesys_server" "myvm" {
platform = "KVM"
datacenter = "Falkenberg"
# ...
}

// NetworkAdapter attached to KVM vm 'myvm'
resource "glesys_networkadapter" "kvm-nic2" {
serverid = glesys_server.myvm.id

name = "pn-internal"
bandwidth = 1000
networkid = glesys_privatenetwork_segment.seg-kvm-fbg.id

}
2 changes: 2 additions & 0 deletions glesys/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func Provider() *schema.Provider {
"glesys_server_disk": resourceGlesysServerDisk(),
"glesys_objectstorage_instance": resourceGlesysObjectStorageInstance(),
"glesys_objectstorage_credential": resourceGlesysObjectStorageCredential(),
"glesys_privatenetwork": resourceGlesysPrivateNetwork(),
"glesys_privatenetwork_segment": resourceGlesysPrivateNetworkSegment(),
"glesys_ip": resourceGlesysIP(),
},
// this will be used to configure the client to communicate with the API
Expand Down
97 changes: 97 additions & 0 deletions glesys/resource_glesys_privatenetwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package glesys

import (
"context"

"github.com/glesys/glesys-go/v8"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceGlesysPrivateNetwork() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGlesysPrivateNetworkCreate,
ReadContext: resourceGlesysPrivateNetworkRead,
UpdateContext: resourceGlesysPrivateNetworkUpdate,
DeleteContext: resourceGlesysPrivateNetworkDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Description: "Create a PrivateNetwork resource.",

Schema: map[string]*schema.Schema{
"name": {
Description: "PrivateNetwork name",
Type: schema.TypeString,
Required: true,
},

"ipv6aggregate": {
Description: "IPv6Aggregate for the PrivateNetwork.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceGlesysPrivateNetworkCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

name := d.Get("name").(string)
network, err := client.PrivateNetworks.Create(ctx, name)
if err != nil {
return diag.Errorf("Error adding privatenetwork %s: %v", name, err)
}

// Set the Id to network.ID
d.SetId(network.ID)

return resourceGlesysPrivateNetworkRead(ctx, d, m)
}

func resourceGlesysPrivateNetworkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

network, err := client.PrivateNetworks.Details(ctx, d.Id())

if err != nil {
diag.Errorf("privatenetwork not found: %v", err)
d.SetId("")
return nil
}

d.Set("ipv6aggregate", network.IPv6Aggregate)
d.Set("name", network.Name)

return nil
}

func resourceGlesysPrivateNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

params := glesys.EditPrivateNetworkParams{ID: d.Id()}

if d.HasChange("name") {
params.Name = d.Get("name").(string)
}

_, err := client.PrivateNetworks.Edit(ctx, params)
if err != nil {
return diag.Errorf("Error updating privatenetwork: %v", err)
}

return resourceGlesysPrivateNetworkRead(ctx, d, m)
}

func resourceGlesysPrivateNetworkDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

err := client.PrivateNetworks.Destroy(ctx, d.Id())
if err != nil {
return diag.Errorf("Error deleting privatenetwork: %v", err)
}
d.SetId("")
return nil
}
137 changes: 137 additions & 0 deletions glesys/resource_glesys_privatenetwork_segment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package glesys

import (
"context"

"github.com/glesys/glesys-go/v8"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceGlesysPrivateNetworkSegment() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGlesysPrivateNetworkSegmentCreate,
ReadContext: resourceGlesysPrivateNetworkSegmentRead,
UpdateContext: resourceGlesysPrivateNetworkSegmentUpdate,
DeleteContext: resourceGlesysPrivateNetworkSegmentDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Description: "Create a PrivateNetwork Segment to connect VM NetworkAdapters.",

Schema: map[string]*schema.Schema{
"name": {
Description: "PrivateNetworkSegment name",
Type: schema.TypeString,
Required: true,
},

"privatenetworkid": {
Description: "PrivateNetwork ID for the Segment.",
Type: schema.TypeString,
Required: true,
},

"platform": {
Description: "PrivateNetworkSegment Platform.",
Type: schema.TypeString,
Required: true,
},

"datacenter": {
Description: "PrivateNetworkSegment Datacenter.",
Type: schema.TypeString,
Required: true,
},

"ipv4subnet": {
Description: "PrivateNetworkSegment IPv4 Subnet.",
Type: schema.TypeString,
Required: true,
},

"ipv6subnet": {
Description: "PrivateNetworkSegment IPv6 Subnet.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceGlesysPrivateNetworkSegmentCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

params := glesys.CreatePrivateNetworkSegmentParams{
PrivateNetworkID: d.Get("privatenetworkid").(string),
Name: d.Get("name").(string),
Datacenter: d.Get("datacenter").(string),
Platform: d.Get("platform").(string),
IPv4Subnet: d.Get("ipv4subnet").(string),
}

name := d.Get("name").(string)
segment, err := client.PrivateNetworks.CreateSegment(ctx, params)
if err != nil {
return diag.Errorf("Error creating segment %s: %v", name, err)
}

// Set the Id to network.ID
d.SetId(segment.ID)

return resourceGlesysPrivateNetworkSegmentRead(ctx, d, m)
}

func resourceGlesysPrivateNetworkSegmentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

// List segments for 'privatenetworkid'
segments, err := client.PrivateNetworks.ListSegments(ctx, d.Get("privatenetworkid").(string))

if err != nil {
diag.Errorf("privatenetwork not found: %v", err)
d.SetId("")
return nil
}

for _, seg := range *segments {
if seg.ID == d.Id() {
d.Set("datacenter", seg.Datacenter)
d.Set("ipv4subnet", seg.IPv4Subnet)
d.Set("ipv6subnet", seg.IPv6Subnet)
d.Set("name", seg.Name)
d.Set("platform", seg.Platform)
}
}

return nil
}

func resourceGlesysPrivateNetworkSegmentUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

params := glesys.EditPrivateNetworkSegmentParams{ID: d.Id()}

if d.HasChange("name") {
params.Name = d.Get("name").(string)
}

_, err := client.PrivateNetworks.EditSegment(ctx, params)
if err != nil {
return diag.Errorf("Error updating segment: %v", err)
}

return resourceGlesysPrivateNetworkSegmentRead(ctx, d, m)
}

func resourceGlesysPrivateNetworkSegmentDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*glesys.Client)

err := client.PrivateNetworks.DestroySegment(ctx, d.Id())
if err != nil {
return diag.Errorf("Error deleting segment: %v", err)
}
d.SetId("")
return nil
}
Loading