Skip to content

Commit

Permalink
Added new datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
arnouthoebreckx committed May 6, 2023
1 parent 53cf086 commit 305f369
Show file tree
Hide file tree
Showing 19 changed files with 557 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SYNOLOGY_ADDRESS=http://aaa.bbb.ccc.dddd:5000 SYNOLOGY_USERNAME=test_user SYNOLO
terraform {
required_providers {
synology = {
version = "0.1"
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ Example:
terraform {
required_providers {
synology = {
version = "0.1"
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
Expand Down
15 changes: 15 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type SynologyClient interface {
UpdateGuest(name string, new_name string) error
DeleteGuest(name string) error
PowerGuest(name string, state bool) error
ReadStorageGuest() (StorageResponse, error)
ReadNetworkGuest() (NetworkResponse, error)
ReadHostGuest() (HostResponse, error)
}

type synologyClient struct {
Expand Down Expand Up @@ -114,6 +117,18 @@ func (client synologyClient) PowerGuest(name string, state bool) error {
return err
}

func (client synologyClient) ReadStorageGuest() (StorageResponse, error) {
return ListStorages(client.apiInfo, client.host, client.sid)
}

func (client synologyClient) ReadNetworkGuest() (NetworkResponse, error) {
return ListNetworks(client.apiInfo, client.host, client.sid)
}

func (client synologyClient) ReadHostGuest() (HostResponse, error) {
return ListHosts(client.apiInfo, client.host, client.sid)
}

func NewClient() SynologyClient {
return &synologyClient{}
}
208 changes: 198 additions & 10 deletions client/virtualmachinemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,50 @@ package client
import (
"encoding/json"
"fmt"
"log"
"strconv"
)

type readReponse struct {
Data Guest `json:"data"`
Success bool `json:"success"`
type readResponse struct {
Data interface{} `json:"data"`
Success bool `json:"success"`
}

type NetworkResponse struct {
Networks []Network `json:"networks"`
}

type Network struct {
NetworkID string `json:"network_id"`
NetworkName string `json:"network_name"`
}

type StorageResponse struct {
Storages []Storage `json:"storages"`
}

type Storage struct {
HostID string `json:"host_id"`
HostName string `json:"host_name"`
Size int `json:"size"`
Status string `json:"status"`
StorageID string `json:"storage_id"`
StorageName string `json:"storage_name"`
Used int `json:"used"`
VolumePath string `json:"volume_path"`
}

type HostResponse struct {
Hosts []Host `json:"hosts"`
}

type Host struct {
FreeCpuCore int `json:"free_cpu_core"`
FreeRamSize int `json:"free_ram_size"`
HostID string `json:"host_id"`
HostName string `json:"host_name"`
Status string `json:"status"`
TotalCpuCore int `json:"total_cpu_core"`
TotalRamSize int `json:"total_ram_size"`
}

type Guest struct {
Expand Down Expand Up @@ -59,7 +96,6 @@ func CreateGuest(apiInfo map[string]InfoData, host string, sid string, name stri
apiName := "SYNO.Virtualization.API.Guest"
info := apiInfo[apiName]

log.Println(vnics)
vnicList := createValidRequestMap(vnics, []string{"mac", "network_id", "network_name"})
vdiskList := createValidRequestMap(vdisks, []string{"create_type", "vdisk_size", "image_id", "image_name"})

Expand Down Expand Up @@ -88,8 +124,6 @@ func CreateGuest(apiInfo map[string]InfoData, host string, sid string, name stri
return CreateGuestResponse{}, err
}

log.Println("Create VMM Guest body" + string(body))

var CreateGuestResponse CreateGuestResponse
json.Unmarshal(body, &CreateGuestResponse)

Expand All @@ -115,14 +149,29 @@ func ReadGuest(apiInfo map[string]InfoData, host string, sid string, name string
return Guest{}, err
}

response := readReponse{}
response := readResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
log.Println(err.Error())
return Guest{}, err
}

return response.Data, nil
guestData, ok := response.Data.(map[string]interface{})
if !ok {
return Guest{}, fmt.Errorf("invalid guest data")
}

guestJson, err := json.Marshal(guestData)
if err != nil {
return Guest{}, err
}

guest := Guest{}
err = json.Unmarshal(guestJson, &guest)
if err != nil {
return Guest{}, err
}

return guest, nil
}

func SetGuest(apiInfo map[string]InfoData, host string, sid string, oldName string, name string, autorun int, description string, vcpuNum int, vramSize int) ([]byte, error) {
Expand Down Expand Up @@ -226,6 +275,132 @@ func PowerGuest(apiInfo map[string]InfoData, host string, sid string, name strin
return statusCode, nil
}

func ListNetworks(apiInfo map[string]InfoData, host string, sid string) (NetworkResponse, error) {
apiName := "SYNO.Virtualization.API.Network"
info := apiInfo[apiName]

queryString := make(map[string]string)
queryString["_sid"] = sid
queryString["api"] = apiName
queryString["version"] = strconv.Itoa(info.MaxVersion)
queryString["method"] = "list"

wsUrl := host + "/webapi/entry.cgi"

_, body, err := HttpCall(wsUrl, queryString)
if err != nil {
return NetworkResponse{}, err
}

response := readResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
return NetworkResponse{}, err
}

storageData, ok := response.Data.(map[string]interface{})
if !ok {
return NetworkResponse{}, fmt.Errorf("invalid NetworkResponse data")
}

storageJson, err := json.Marshal(storageData)
if err != nil {
return NetworkResponse{}, err
}

networkResponse := NetworkResponse{}
err = json.Unmarshal(storageJson, &networkResponse)
if err != nil {
return NetworkResponse{}, err
}

return networkResponse, nil
}

func ListStorages(apiInfo map[string]InfoData, host string, sid string) (StorageResponse, error) {
apiName := "SYNO.Virtualization.API.Storage"
info := apiInfo[apiName]

queryString := make(map[string]string)
queryString["_sid"] = sid
queryString["api"] = apiName
queryString["version"] = strconv.Itoa(info.MaxVersion)
queryString["method"] = "list"

wsUrl := host + "/webapi/entry.cgi"

_, body, err := HttpCall(wsUrl, queryString)
if err != nil {
return StorageResponse{}, err
}

response := readResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
return StorageResponse{}, err
}

storageData, ok := response.Data.(map[string]interface{})
if !ok {
return StorageResponse{}, fmt.Errorf("invalid StorageResponse data")
}

storageJson, err := json.Marshal(storageData)
if err != nil {
return StorageResponse{}, err
}

storageResponse := StorageResponse{}
err = json.Unmarshal(storageJson, &storageResponse)
if err != nil {
return StorageResponse{}, err
}

return storageResponse, nil
}

func ListHosts(apiInfo map[string]InfoData, host string, sid string) (HostResponse, error) {
apiName := "SYNO.Virtualization.API.Host"
info := apiInfo[apiName]

queryString := make(map[string]string)
queryString["_sid"] = sid
queryString["api"] = apiName
queryString["version"] = strconv.Itoa(info.MaxVersion)
queryString["method"] = "list"

wsUrl := host + "/webapi/entry.cgi"

_, body, err := HttpCall(wsUrl, queryString)
if err != nil {
return HostResponse{}, err
}

response := readResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
return HostResponse{}, err
}

storageData, ok := response.Data.(map[string]interface{})
if !ok {
return HostResponse{}, fmt.Errorf("invalid HostResponse data")
}

storageJson, err := json.Marshal(storageData)
if err != nil {
return HostResponse{}, err
}

hostResponse := HostResponse{}
err = json.Unmarshal(storageJson, &hostResponse)
if err != nil {
return HostResponse{}, err
}

return hostResponse, nil
}

func (g Guest) String() string {
str := fmt.Sprintf("Guest:\n\tGuestName: %s\n\tGuestId: %s\n\tAutorun: %d\n\tDescription: %s\n\tStatus: %s\n\tStorageName: %s\n\tStorageId: %s\n\tVcpuNum: %d\n\tVramSize: %d\n\tVdisks: [\n", g.GuestName, g.GuestId, g.Autorun, g.Description, g.Status, g.StorageName, g.StorageId, g.VcpuNum, g.VramSize)
for _, vdisk := range g.Vdisks {
Expand All @@ -247,6 +422,19 @@ func (vdisk VDisk) String() string {
return fmt.Sprintf("VDisk:\n\tController: %d\n\tUnmap: %t\n\tVdiskId: %s\n\tVdiskSize: %d", vdisk.Controller, vdisk.Unmap, vdisk.VdiskId, vdisk.VdiskSize)
}

func (sr StorageResponse) String() string {
str := "Storages:\n\t"
for _, storage := range sr.Storages {
str += fmt.Sprintf("\t\t%s\n", storage.String())
}
return str
}

func (s Storage) String() string {
return fmt.Sprintf("HostID: %s\nHostName: %s\nSize: %d\nStatus: %s\nStorageID: %s\nStorageName: %s\nUsed: %d\nVolumePath: %s\n",
s.HostID, s.HostName, s.Size, s.Status, s.StorageID, s.StorageName, s.Used, s.VolumePath)
}

func createValidRequestMap(input []interface{}, allowedKeys []string) []map[string]interface{} {
var output []map[string]interface{}

Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/vmm_guest.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description: |-
terraform {
required_providers {
synology = {
version = "0.1"
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description: |-
terraform {
required_providers {
synology = {
version = "0.1"
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description: |-
terraform {
required_providers {
synology = {
version = "0.1"
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/vmm_guest.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description: |-
terraform {
required_providers {
synology = {
version = "0.1"
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/data-sources/synology_vmm_guest/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
terraform {
required_providers {
synology = {
version = "0.1"
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
Expand All @@ -15,7 +15,7 @@ provider "synology" {
}

resource "synology_vmm_guest" "my-guest" {
autorun = 2
autorun = 2
guest_name = "terraform-guest"
description = "Virtual machine setup with terraform"
storage_name = "synology - VM Storage 1"
Expand Down
18 changes: 18 additions & 0 deletions examples/data-sources/synology_vmm_guest_host/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
synology = {
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
}

provider "synology" {
url = "<SYNOLOGY_ADDRESS>"
username = "<SYNOLOGY_USERNAME>"
password = "<SYNOLOGY_PASSWORD>"
# these variables can be set as env vars in SYNOLOGY_ADDRESS SYNOLOGY_USERNAME and SYNOLOGY_PASSWORD
}

data "synology_vmm_guest_host" "my-guest" {
}
18 changes: 18 additions & 0 deletions examples/data-sources/synology_vmm_guest_network/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
synology = {
version = "0.2.0"
source = "github.com/arnouthoebreckx/synology"
}
}
}

provider "synology" {
url = "<SYNOLOGY_ADDRESS>"
username = "<SYNOLOGY_USERNAME>"
password = "<SYNOLOGY_PASSWORD>"
# these variables can be set as env vars in SYNOLOGY_ADDRESS SYNOLOGY_USERNAME and SYNOLOGY_PASSWORD
}

data "synology_vmm_guest_network" "my-guest" {
}
Loading

0 comments on commit 305f369

Please sign in to comment.