Upclapi/prices.go
2021-08-28 14:45:05 +01:00

279 lines
12 KiB
Go

package main
import (
"fmt"
"math"
// "log"
// "strings"
// "encoding/json"
// "github.com/UpCloudLtd/upcloud-go-api/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/upcloud/service"
// "github.com/UpCloudLtd/upcloud-go-api/upcloud/request"
// "gocloud.dev/server"
// "gocloud.dev/server"
// "github.com/davecgh/go-spew/spew"
)
func dataPriceResource(s *service.Service, dataprices DataPrices, zone string, target string) (*Price,error) {
resource_price := &Price{}
resource_zone := PricesZone{}
for _,price_zone := range dataprices.Prices.Zone {
if price_zone.Name == zone {
resource_zone = price_zone
break
}
}
if resource_zone.Name == zone {
switch target {
case "firewall":
resource_price = &resource_zone.Firewall
case "io_request_backup":
resource_price = &resource_zone.IORequestBackup
case "io_request_maxiops":
resource_price = &resource_zone.IORequestMaxIOPS
case "ipv4_address":
resource_price = &resource_zone.IPv4Address
case "ipv6_address":
resource_price = &resource_zone.IPv6Address
case "managed_database_1x1xCPU-2GB-25GB":
resource_price = &resource_zone.ManagedDatabase1x1CPU2GB25GB
case "managed_database_1x2xCPU-4GB-100GB":
resource_price = &resource_zone.ManagedDatabase1x2CPU4GB100GB
case "managed_database_1x2xCPU-4GB-50GB":
resource_price = &resource_zone.ManagedDatabase1x2CPU4GB50GB
case "managed_database_2x2xCPU-4GB-100GB":
resource_price = &resource_zone.ManagedDatabase2x2CPU4GB100GB
case "managed_database_2x2xCPU-4GB-50GB":
resource_price = &resource_zone.ManagedDatabase2x2CPU4GB50GB
case "managed_database_2x4xCPU-8GB-100GB":
resource_price = &resource_zone.ManagedDatabase2x4CPU8GB100GB
case "managed_database_2x4xCPU-8GB-50GB":
resource_price = &resource_zone.ManagedDatabase2x4CPU8GB50GB
case "managed_database_2x6xCPU-16GB-100GB":
resource_price = &resource_zone.ManagedDatabase2x6CPU16GB100GB
case "managed_database_2x6xCPU-16GB-250GB":
resource_price = &resource_zone.ManagedDatabase2x6CPU16GB50GB
case "managed_database_2x8xCPU-32GB-100GB":
resource_price = &resource_zone.ManagedDatabase2x8CPU32GB100GB
case "managed_database_2x8xCPU-32GB-250GB":
resource_price = &resource_zone.ManagedDatabase2x8CPU22GB250GB
case "managed_database_3x2xCPU-4GB-100GB":
resource_price = &resource_zone.ManagedDatabase3x2CPU4GB100GB
case "managed_database_3x2xCPU-4GB-200GB":
resource_price = &resource_zone.ManagedDatabase3x2CPU4GB200GB
case "managed_database_3x4xCPU-8GB-100GB":
resource_price = &resource_zone.ManagedDatabase3x4CPU8GB100GB
case "managed_database_3x4xCPU-8GB-200GB":
resource_price = &resource_zone.ManagedDatabase3x4CPU8GB200GB
case "managed_database_3x6xCPU-16GB-200GB":
resource_price = &resource_zone.ManagedDatabase3x6CPU16GB200GB
case "managed_database_3x6xCPU-16GB-500GB":
resource_price = &resource_zone.ManagedDatabase3x6CPU16GB500GB
case "managed_database_3x8xCPU-32GB-200GB":
resource_price = &resource_zone.ManagedDatabase3x8CPU32GB200GB
case "managed_database_3x8xCPU-32GB-500GB":
resource_price = &resource_zone.ManagedDatabase3x8CPU32GB500GB
case "network_private_vlan":
resource_price = &resource_zone.NetworkPrivateVLAN
case "object_storage_1TB":
resource_price = &resource_zone.ObjectStorage1TB
case "object_storage_250GB":
resource_price = &resource_zone.ObjectStorage250GB
case "object_storage_500GB":
resource_price = &resource_zone.ObjectStorage500GB
case "public_ipv4_bandwidth_in":
resource_price = &resource_zone.PublicIPv4BandwidthIn
case "public_ipv4_bandwidth_out":
resource_price = &resource_zone.PublicIPv4BandwidthOut
case "public_ipv6_bandwidth_in":
resource_price = &resource_zone.PublicIPv6BandwidthIn
case "public_ipv6_bandwidth_out":
resource_price = &resource_zone.PublicIPv6BandwidthOut
case "server_core":
resource_price = &resource_zone.ServerCore
case "server_memory":
resource_price = &resource_zone.ServerMemory
case "server_plan_6xCPU-8GB":
resource_price = &resource_zone.ServerPlan6xCPU8GB
case "server_plan_12xCPU-48GB":
resource_price = &resource_zone.ServerPlan12xCPU48GB
case "server_plan_16xCPU-64GB":
resource_price = &resource_zone.ServerPlan16xCPU64GB
case "server_plan_1xCPU-1GB":
resource_price = &resource_zone.ServerPlan1xCPU1GB
case "server_plan_1xCPU-2GB":
resource_price = &resource_zone.ServerPlan1xCPU2GB
case "server_plan_20xCPU-128GB":
resource_price = &resource_zone.ServerPlan20xCPU128GB
case "server_plan_20xCPU-96GB":
resource_price = &resource_zone.ServerPlan20xCPU96GB
case "server_plan_2xCPU-4GB":
resource_price = &resource_zone.ServerPlan2xCPU4GB
case "server_plan_4xCPU-8GB":
resource_price = &resource_zone.ServerPlan4xCPU8GB
case "server_plan_6xCPU-16GB":
resource_price = &resource_zone.ServerPlan6xCPU16GB
case "server_plan_8xCPU-32GB":
resource_price = &resource_zone.ServerPlan8xCPU32GB
case "simple_backup_dailies_12xCPU-48GB":
resource_price = &resource_zone.SimpleBackupDailies12xCPU48GB
case "simple_backup_dailies_16xCPU-64GB":
resource_price = &resource_zone.SimpleBackupDailies16xCPU64GB
case "simple_backup_dailies_1xCPU-1GB":
resource_price = &resource_zone.SimpleBackupDailiesx1CPU1GB
case "simple_backup_dailies_1xCPU-2GB":
resource_price = &resource_zone.SimpleBackupDailies1xCPU2GB
case "simple_backup_dailies_20xCPU-128GB":
resource_price = &resource_zone.SimpleBackupDailies20xCPU128GB
case "simple_backup_dailies_20xCPU-96GB":
resource_price = &resource_zone.SimpleBackupDailies20xCPU96GB
case "simple_backup_dailies_2xCPU-4GB":
resource_price = &resource_zone.SimpleBackupDailies2xCPU4GB
case "simple_backup_dailies_4xCPU-8GB":
resource_price = &resource_zone.SimpleBackupDailies4xCPU8GB
case "simple_backup_dailies_6xCPU-16GB":
resource_price = &resource_zone.SimpleBackupDailies6xCPU16GB
case "simple_backup_dailies_8xCPU-32GB":
resource_price = &resource_zone.SimpleBackupDailies8xCPU32GB
case "simple_backup_extra_dailies":
resource_price = &resource_zone.SimpleBackupExtraDailies
case "simple_backup_extra_monthlies":
resource_price = &resource_zone.SimpleBackupExtraMontlies
case "simple_backup_extra_weeklies":
resource_price = &resource_zone.SimpleBackupExtraWeeklies
case "simple_backup_monthlies_12xCPU-48GB":
resource_price = &resource_zone.SimpleBackupMonthlies12xCPU48GB
case "simple_backup_monthlies_16xCPU-64GB":
resource_price = &resource_zone.SimpleBackupMonthlies16xCPU64GB
case "simple_backup_monthlies_1xCPU-1GB":
resource_price = &resource_zone.SimpleBackupMonthlies1xCPU1GB
case "simple_backup_monthlies_1xCPU-2GB":
resource_price = &resource_zone.SimpleBackupMonthlies1xCPU2GB
case "simple_backup_monthlies_20xCPU-128GB":
resource_price = &resource_zone.SimpleBackupMonthlies20xCPU128GB
case "simple_backup_monthlies_20xCPU-96GB":
resource_price = &resource_zone.SimpleBackupMonthlies20xCPU96GB
case "simple_backup_monthlies_2xCPU-4GB":
resource_price = &resource_zone.SimpleBackupMonthlies2xCPU4GB
case "simple_backup_monthlies_4xCPU-8GB":
resource_price = &resource_zone.SimpleBackupMonthlies4xCPU8GB
case "simple_backup_monthlies_6xCPU-16GB":
resource_price = &resource_zone.SimpleBackupMonthlies6xCPU16GB
case "simple_backup_monthlies_8xCPU-32GB":
resource_price = &resource_zone.SimpleBackupMonthlies8xCPU32GB
case "simple_backup_weeklies_12xCPU-48GB":
resource_price = &resource_zone.SimpleBackupWeeklies12xCPU48GB
case "simple_backup_weeklies_16xCPU-64GB":
resource_price = &resource_zone.SimpleBackupWeeklies16xCPU64GB
case "simple_backup_weeklies_1xCPU-1GB":
resource_price = &resource_zone.SimpleBackupWeeklies1xCPU1GB
case "simple_backup_weeklies_1xCPU-2GB":
resource_price = &resource_zone.SimpleBackupWeeklies1xCPU2GB
case "simple_backup_weeklies_20xCPU-128GB":
resource_price = &resource_zone.SimpleBackupWeeklies20xCPU128GB
case "simple_backup_weeklies_20xCPU-96GB":
resource_price = &resource_zone.SimpleBackupWeeklies20xCPU96GB
case "simple_backup_weeklies_2xCPU-4GB":
resource_price = &resource_zone.SimpleBackupWeeklies2xCPU4GB
case "simple_backup_weeklies_4xCPU-8GB":
resource_price = &resource_zone.SimpleBackupWeeklies4xCPU8GB
case "simple_backup_weeklies_6xCPU-16GB":
resource_price = &resource_zone.SimpleBackupWeeklies6xCPU16GB
case "simple_backup_weeklies_8xCPU-32GB":
resource_price = &resource_zone.SimpleBackupWeeklies8xCPU32GB
case "storage_hdd":
resource_price = &resource_zone.StorageHDD
case "storage_backup":
resource_price = &resource_zone.StorageBackup
case "storage_maxiops":
resource_price = &resource_zone.StorageMaxIOPS
case "storage_template":
resource_price = &resource_zone.StorageTemplate
default:
//fmt.Fprintf(os.Stderr, "Resource price %s in %s NOT FOUND",target,zone)
return resource_price, fmt.Errorf("Resource price %s in %s NOT FOUND",target,zone)
}
resource_price.Hour = (resource_price.Price / 100)
// resource_price.Month = (resource_price.Hour * MONTH_HOURS)
resource_price.Month = math.Round(resource_price.Hour * MONTH_HOURS)
// fmt.Printf("Resource %s price in %s: %#v\n", target, zone, resource_price)
// fmt.Printf("Resource %s price in %s: %d %.4f\n", target, zone, resource_price.Amount, resource_price.Price)
}
// enc := json.NewEncoder(os.Stdout)
// enc.Encode(resource_price)
return resource_price, nil
}
/*
// UpCloud API is incomplete to make this work
// priceZones := upcloud.PriceZones{}
func upcloud_priceResource(s *service.Service, zone string, target string) (upcloud.Price,error) {
pricesZones,err:= s.GetPriceZones()
resource_price := new(upcloud.Price)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to get prices zones: %#v\n", err)
return *resource_price,err
}
resource_zone := upcloud.PriceZone{}
for _,price_zone := range pricesZones.PriceZones {
if price_zone.Name == zone {
resource_zone = price_zone
break
}
}
if resource_zone.Name == zone {
switch target {
case "firewall":
resource_price = resource_zone.Firewall
case "io_request_backup":
resource_price = resource_zone.IORequestBackup
case "io_request_maxiops":
resource_price = resource_zone.IORequestMaxIOPS
case "ipv4_address":
resource_price = resource_zone.IPv4Address
case "ipv6_address":
resource_price = resource_zone.IPv6Address
case "public_ipv4_bandwidth_in":
resource_price = resource_zone.PublicIPv4BandwidthIn
case "public_ipv4_bandwidth_out":
resource_price = resource_zone.PublicIPv4BandwidthOut
case "public_ipv6_bandwidth_in":
resource_price = resource_zone.PublicIPv6BandwidthIn
case "public_ipv6_bandwidth_out":
resource_price = resource_zone.PublicIPv6BandwidthOut
case "server_core":
resource_price = resource_zone.ServerCore
case "server_memory":
resource_price = resource_zone.ServerMemory
case "server_plan_1xCPU-1GB":
resource_price = resource_zone.ServerPlan1xCPU1GB
case "server_plan_2xCPU-2GB":
resource_price = resource_zone.ServerPlan2xCPU2GB
case "server_plan_2xCPU-4GB":
resource_price.Price = 2.9761
case "server_plan_4xCPU-4GB":
resource_price = resource_zone.ServerPlan4xCPU4GB
case "server_plan_6xCPU-8GB":
resource_price = resource_zone.ServerPlan6xCPU8GB
case "storage_backup":
resource_price = resource_zone.StorageBackup
case "storage_maxiops":
resource_price = resource_zone.StorageMaxIOPS
case "storage_template":
resource_price = resource_zone.StorageTemplate
default:
//fmt.Fprintf(os.Stderr, "Resource price %s in %s NOT FOUND",target,zone)
return *resource_price, fmt.Errorf("Resource price %s in %s NOT FOUND",target,zone)
}
// fmt.Printf("Resource %s price in %s: %#v\n", target, zone, resource_price)
// fmt.Printf("Resource %s price in %s: %d %.4f\n", target, zone, resource_price.Amount, resource_price.Price)
}
// enc := json.NewEncoder(os.Stdout)
// enc.Encode(resource_price)
return *resource_price, nil
}
*/