From 74c1bd488f6c3df7c255d7b6cd68fd2eac0549b2 Mon Sep 17 00:00:00 2001 From: pauhull Date: Thu, 23 Nov 2023 13:54:57 +0100 Subject: [PATCH] refactor: automatically generate schema conversions with goverter --- .gitattributes | 2 +- .github/workflows/ci.yml | 16 + go.mod | 2 + go.sum | 69 + hcloud/certificate_test.go | 2 +- hcloud/schema.go | 1317 +++---------------- hcloud/schema_gen.go | 878 +++++++++++++ hcloud/schema_test.go | 2014 +++++++---------------------- hcloud/zz_schema.go | 2468 ++++++++++++++++++++++++++++++++++++ script/generate_schema.sh | 21 + tools.go | 1 + 11 files changed, 4116 insertions(+), 2674 deletions(-) create mode 100644 hcloud/schema_gen.go create mode 100755 hcloud/zz_schema.go create mode 100644 script/generate_schema.sh diff --git a/.gitattributes b/.gitattributes index 2d995061b..b22d9b482 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -hcloud/zz_*_iface.go linguist-generated=true +hcloud/zz_*.go linguist-generated=true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2bf1cae48..3afeebf87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,3 +34,19 @@ jobs: go-version: ${{ matrix.go-version }} - run: go test -v ./... + + generate: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 + with: + go-version: "1.21" + cache: false + + - run: go generate ./... + + - run: git diff --exit-code + + - if: failure() + run: echo "::error::Check failed, please run 'go generate ./...' and commit the changes." \ No newline at end of file diff --git a/go.mod b/go.mod index 267c92266..f59ea0a67 100644 --- a/go.mod +++ b/go.mod @@ -17,9 +17,11 @@ require ( require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dave/jennifer v1.6.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 // indirect + github.com/jmattheis/goverter v1.2.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 7744bbac6..7bb27e777 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,16 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/dave/astrid v0.0.0-20170323122508-8c2895878b14/go.mod h1:Sth2QfxfATb/nW4EsrSi2KyJmbcniZ8TgTaji17D6ms= +github.com/dave/brenda v1.1.0/go.mod h1:4wCUr6gSlu5/1Tk7akE5X7UorwiQ8Rij0SKH3/BGMOM= +github.com/dave/courtney v0.3.0/go.mod h1:BAv3hA06AYfNUjfjQr+5gc6vxeBVOupLqrColj+QSD8= +github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ= +github.com/dave/jennifer v1.6.0 h1:MQ/6emI2xM7wt0tJzJzyUik2Q3Tcn2eE0vtYgh4GPVI= +github.com/dave/jennifer v1.6.0/go.mod h1:AxTG893FiZKqxy3FP1kL80VMshSMuz2G+EgvszgGRnk= +github.com/dave/kerr v0.0.0-20170318121727-bc25dd6abe8e/go.mod h1:qZqlPyPvfsDJt+3wHJ1EvSXDuVjFTK0j2p/ca+gtsb8= +github.com/dave/patsy v0.0.0-20210517141501-957256f50cba/go.mod h1:qfR88CgEGLoiqDaE+xxDCi5QA5v4vUoW0UCX2Nd5Tlc= +github.com/dave/rebecca v0.9.1/go.mod h1:N6XYdMD/OKw3lkF3ywh8Z6wPGuwNFDNtWYEMFWEmXBA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -14,11 +24,20 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 h1:dAOsPLhnBzIyxu0VvmnKjlNcIlgMK+erD6VRHDtweMI= github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jmattheis/goverter v1.1.0 h1:HcIxgkUJ44Ah2JYCmdmippjUwQH4vJxclxWUzmaKCxA= +github.com/jmattheis/goverter v1.1.0/go.mod h1:Il/E+0riIfIgRBUpM+Fnh2s8/sJhMp5NeDZZenTd6S4= +github.com/jmattheis/goverter v1.1.1 h1:CUukRtL/K9beOSvcV76oxmATYmmMK5rtW2CfkBu9BKc= +github.com/jmattheis/goverter v1.1.1/go.mod h1:Il/E+0riIfIgRBUpM+Fnh2s8/sJhMp5NeDZZenTd6S4= +github.com/jmattheis/goverter v1.1.2-0.20231122201214-b16152097a10 h1:tUHxPZXSRAWfmfppfurlG96yQO33xJ1Z+OQnIKh4iFM= +github.com/jmattheis/goverter v1.1.2-0.20231122201214-b16152097a10/go.mod h1:Il/E+0riIfIgRBUpM+Fnh2s8/sJhMp5NeDZZenTd6S4= +github.com/jmattheis/goverter v1.2.0 h1:hespXDXi19SWVZKWJyWn0/SSJigEBg4Mjl8BpR2MaHE= +github.com/jmattheis/goverter v1.2.0/go.mod h1:Il/E+0riIfIgRBUpM+Fnh2s8/sJhMp5NeDZZenTd6S4= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q= @@ -31,33 +50,83 @@ github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwa github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/vburenin/ifacemaker v1.2.1 h1:3Vq8B/bfBgjWTkv+jDg4dVL1KHt3k1K4lO7XRxYA2sk= github.com/vburenin/ifacemaker v1.2.1/go.mod h1:5WqrzX2aD7/hi+okBjcaEQJMg4lDGrpuEX3B8L4Wgrs= +github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k= golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hcloud/certificate_test.go b/hcloud/certificate_test.go index 8bc11d329..0bb2cdc22 100644 --- a/hcloud/certificate_test.go +++ b/hcloud/certificate_test.go @@ -448,7 +448,7 @@ func TestCertificateClient_RetryIssuance(t *testing.T) { assert.Equal(t, "POST", r.Method) resp := schema.CertificateIssuanceRetryResponse{ - Action: schema.Action{ID: 1}, + Action: schema.Action{ID: 1, Resources: []schema.ActionResourceReference{}}, } err := json.NewEncoder(w).Encode(resp) assert.NoError(t, err) diff --git a/hcloud/schema.go b/hcloud/schema.go index b7f046f09..9ded8792b 100644 --- a/hcloud/schema.go +++ b/hcloud/schema.go @@ -1,1271 +1,370 @@ package hcloud import ( - "fmt" - "net" - "strconv" - "time" - "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" ) // This file provides converter functions to convert models in the // schema package to models in the hcloud package and vice versa. +var c converter = &converterImpl{} + // ActionFromSchema converts a schema.Action to an Action. func ActionFromSchema(s schema.Action) *Action { - action := &Action{ - ID: s.ID, - Status: ActionStatus(s.Status), - Command: s.Command, - Progress: s.Progress, - Started: s.Started, - Resources: []*ActionResource{}, - } - if s.Finished != nil { - action.Finished = *s.Finished - } - if s.Error != nil { - action.ErrorCode = s.Error.Code - action.ErrorMessage = s.Error.Message - } - for _, r := range s.Resources { - action.Resources = append(action.Resources, &ActionResource{ - ID: r.ID, - Type: ActionResourceType(r.Type), - }) - } - return action + return c.ActionFromSchema(s) +} + +// SchemaFromAction converts an Action to a schema.Action. +func SchemaFromAction(a *Action) schema.Action { + return c.SchemaFromAction(a) } // ActionsFromSchema converts a slice of schema.Action to a slice of Action. func ActionsFromSchema(s []schema.Action) []*Action { - actions := make([]*Action, len(s)) - for i, a := range s { - actions[i] = ActionFromSchema(a) - } - return actions + return c.ActionsFromSchema(s) +} + +// SchemaFromActions converts a slice of Action to a slice of schema.Action. +func SchemaFromActions(a []*Action) []schema.Action { + return c.SchemaFromActions(a) } // FloatingIPFromSchema converts a schema.FloatingIP to a FloatingIP. func FloatingIPFromSchema(s schema.FloatingIP) *FloatingIP { - f := &FloatingIP{ - ID: s.ID, - Type: FloatingIPType(s.Type), - HomeLocation: LocationFromSchema(s.HomeLocation), - Created: s.Created, - Blocked: s.Blocked, - Protection: FloatingIPProtection{ - Delete: s.Protection.Delete, - }, - Name: s.Name, - } - if s.Description != nil { - f.Description = *s.Description - } - if s.Server != nil { - f.Server = &Server{ID: *s.Server} - } - if f.Type == FloatingIPTypeIPv4 { - f.IP = net.ParseIP(s.IP) - } else { - f.IP, f.Network, _ = net.ParseCIDR(s.IP) - } - f.DNSPtr = map[string]string{} - for _, entry := range s.DNSPtr { - f.DNSPtr[entry.IP] = entry.DNSPtr - } - f.Labels = map[string]string{} - for key, value := range s.Labels { - f.Labels[key] = value - } - return f + return c.FloatingIPFromSchema(s) +} + +// SchemaFromFloatingIP converts a FloatingIP to a schema.FloatingIP. +func SchemaFromFloatingIP(f *FloatingIP) schema.FloatingIP { + return c.SchemaFromFloatingIP(f) } // PrimaryIPFromSchema converts a schema.PrimaryIP to a PrimaryIP. func PrimaryIPFromSchema(s schema.PrimaryIP) *PrimaryIP { - f := &PrimaryIP{ - ID: s.ID, - Type: PrimaryIPType(s.Type), - AutoDelete: s.AutoDelete, - - Created: s.Created, - Blocked: s.Blocked, - Protection: PrimaryIPProtection{ - Delete: s.Protection.Delete, - }, - Name: s.Name, - AssigneeType: s.AssigneeType, - AssigneeID: s.AssigneeID, - Datacenter: DatacenterFromSchema(s.Datacenter), - } - - if f.Type == PrimaryIPTypeIPv4 { - f.IP = net.ParseIP(s.IP) - } else { - f.IP, f.Network, _ = net.ParseCIDR(s.IP) - } - f.DNSPtr = map[string]string{} - for _, entry := range s.DNSPtr { - f.DNSPtr[entry.IP] = entry.DNSPtr - } - f.Labels = map[string]string{} - for key, value := range s.Labels { - f.Labels[key] = value - } - return f + return c.PrimaryIPFromSchema(s) +} + +// SchemaFromPrimaryIP converts a PrimaryIP to a schema.PrimaryIP. +func SchemaFromPrimaryIP(p *PrimaryIP) schema.PrimaryIP { + return c.SchemaFromPrimaryIP(p) } // ISOFromSchema converts a schema.ISO to an ISO. func ISOFromSchema(s schema.ISO) *ISO { - iso := &ISO{ - ID: s.ID, - Name: s.Name, - Description: s.Description, - Type: ISOType(s.Type), - Deprecated: s.Deprecated, - DeprecatableResource: DeprecatableResource{ - DeprecationFromSchema(s.Deprecation), - }, - } - if s.Architecture != nil { - iso.Architecture = Ptr(Architecture(*s.Architecture)) - } - return iso + return c.ISOFromSchema(s) +} + +// SchemaFromISO converts an ISO to a schema.ISO. +func SchemaFromISO(i *ISO) schema.ISO { + return c.SchemaFromISO(i) } // LocationFromSchema converts a schema.Location to a Location. func LocationFromSchema(s schema.Location) *Location { - return &Location{ - ID: s.ID, - Name: s.Name, - Description: s.Description, - Country: s.Country, - City: s.City, - Latitude: s.Latitude, - Longitude: s.Longitude, - NetworkZone: NetworkZone(s.NetworkZone), - } + return c.LocationFromSchema(s) +} + +// SchemaFromLocation converts a Location to a schema.Location. +func SchemaFromLocation(l *Location) schema.Location { + return c.SchemaFromLocation(l) } // DatacenterFromSchema converts a schema.Datacenter to a Datacenter. func DatacenterFromSchema(s schema.Datacenter) *Datacenter { - d := &Datacenter{ - ID: s.ID, - Name: s.Name, - Description: s.Description, - Location: LocationFromSchema(s.Location), - ServerTypes: DatacenterServerTypes{ - Available: []*ServerType{}, - Supported: []*ServerType{}, - }, - } - for _, t := range s.ServerTypes.Available { - d.ServerTypes.Available = append(d.ServerTypes.Available, &ServerType{ID: t}) - } - for _, t := range s.ServerTypes.Supported { - d.ServerTypes.Supported = append(d.ServerTypes.Supported, &ServerType{ID: t}) - } - return d + return c.DatacenterFromSchema(s) +} + +// SchemaFromDatacenter converts a Datacenter to a schema.Datacenter. +func SchemaFromDatacenter(d *Datacenter) schema.Datacenter { + return c.SchemaFromDatacenter(d) } // ServerFromSchema converts a schema.Server to a Server. func ServerFromSchema(s schema.Server) *Server { - server := &Server{ - ID: s.ID, - Name: s.Name, - Status: ServerStatus(s.Status), - Created: s.Created, - PublicNet: ServerPublicNetFromSchema(s.PublicNet), - ServerType: ServerTypeFromSchema(s.ServerType), - IncludedTraffic: s.IncludedTraffic, - RescueEnabled: s.RescueEnabled, - Datacenter: DatacenterFromSchema(s.Datacenter), - Locked: s.Locked, - PrimaryDiskSize: s.PrimaryDiskSize, - Protection: ServerProtection{ - Delete: s.Protection.Delete, - Rebuild: s.Protection.Rebuild, - }, - } - if s.Image != nil { - server.Image = ImageFromSchema(*s.Image) - } - if s.BackupWindow != nil { - server.BackupWindow = *s.BackupWindow - } - if s.OutgoingTraffic != nil { - server.OutgoingTraffic = *s.OutgoingTraffic - } - if s.IngoingTraffic != nil { - server.IngoingTraffic = *s.IngoingTraffic - } - if s.ISO != nil { - server.ISO = ISOFromSchema(*s.ISO) - } - server.Labels = map[string]string{} - for key, value := range s.Labels { - server.Labels[key] = value - } - for _, id := range s.Volumes { - server.Volumes = append(server.Volumes, &Volume{ID: id}) - } - for _, privNet := range s.PrivateNet { - server.PrivateNet = append(server.PrivateNet, ServerPrivateNetFromSchema(privNet)) - } - if s.PlacementGroup != nil { - server.PlacementGroup = PlacementGroupFromSchema(*s.PlacementGroup) - } - return server + return c.ServerFromSchema(s) +} + +// SchemaFromServer converts a Server to a schema.Server. +func SchemaFromServer(s *Server) schema.Server { + return c.SchemaFromServer(s) } // ServerPublicNetFromSchema converts a schema.ServerPublicNet to a ServerPublicNet. func ServerPublicNetFromSchema(s schema.ServerPublicNet) ServerPublicNet { - publicNet := ServerPublicNet{ - IPv4: ServerPublicNetIPv4FromSchema(s.IPv4), - IPv6: ServerPublicNetIPv6FromSchema(s.IPv6), - } - for _, id := range s.FloatingIPs { - publicNet.FloatingIPs = append(publicNet.FloatingIPs, &FloatingIP{ID: id}) - } - for _, fw := range s.Firewalls { - publicNet.Firewalls = append(publicNet.Firewalls, - &ServerFirewallStatus{ - Firewall: Firewall{ID: fw.ID}, - Status: FirewallStatus(fw.Status)}, - ) - } - return publicNet + return c.ServerPublicNetFromSchema(s) +} + +// SchemaFromServerPublicNet converts a ServerPublicNet to a schema.ServerPublicNet. +func SchemaFromServerPublicNet(s ServerPublicNet) schema.ServerPublicNet { + return c.SchemaFromServerPublicNet(s) } // ServerPublicNetIPv4FromSchema converts a schema.ServerPublicNetIPv4 to // a ServerPublicNetIPv4. func ServerPublicNetIPv4FromSchema(s schema.ServerPublicNetIPv4) ServerPublicNetIPv4 { - return ServerPublicNetIPv4{ - ID: s.ID, - IP: net.ParseIP(s.IP), - Blocked: s.Blocked, - DNSPtr: s.DNSPtr, - } + return c.ServerPublicNetIPv4FromSchema(s) +} + +// SchemaFromServerPublicNetIPv4 converts a ServerPublicNetIPv4 to +// a schema.ServerPublicNetIPv4. +func SchemaFromServerPublicNetIPv4(s ServerPublicNetIPv4) schema.ServerPublicNetIPv4 { + return c.SchemaFromServerPublicNetIPv4(s) } // ServerPublicNetIPv6FromSchema converts a schema.ServerPublicNetIPv6 to // a ServerPublicNetIPv6. func ServerPublicNetIPv6FromSchema(s schema.ServerPublicNetIPv6) ServerPublicNetIPv6 { - ipv6 := ServerPublicNetIPv6{ - ID: s.ID, - Blocked: s.Blocked, - DNSPtr: map[string]string{}, - } - ipv6.IP, ipv6.Network, _ = net.ParseCIDR(s.IP) + return c.ServerPublicNetIPv6FromSchema(s) +} - for _, dnsPtr := range s.DNSPtr { - ipv6.DNSPtr[dnsPtr.IP] = dnsPtr.DNSPtr - } - return ipv6 +// SchemaFromServerPublicNetIPv6 converts a ServerPublicNetIPv6 to +// a schema.ServerPublicNetIPv6. +func SchemaFromServerPublicNetIPv6(s ServerPublicNetIPv6) schema.ServerPublicNetIPv6 { + return c.SchemaFromServerPublicNetIPv6(s) } // ServerPrivateNetFromSchema converts a schema.ServerPrivateNet to a ServerPrivateNet. func ServerPrivateNetFromSchema(s schema.ServerPrivateNet) ServerPrivateNet { - n := ServerPrivateNet{ - Network: &Network{ID: s.Network}, - IP: net.ParseIP(s.IP), - MACAddress: s.MACAddress, - } - for _, ip := range s.AliasIPs { - n.Aliases = append(n.Aliases, net.ParseIP(ip)) - } - return n + return c.ServerPrivateNetFromSchema(s) +} + +// SchemaFromServerPrivateNet converts a ServerPrivateNet to a schema.ServerPrivateNet. +func SchemaFromServerPrivateNet(s ServerPrivateNet) schema.ServerPrivateNet { + return c.SchemaFromServerPrivateNet(s) } // ServerTypeFromSchema converts a schema.ServerType to a ServerType. func ServerTypeFromSchema(s schema.ServerType) *ServerType { - st := &ServerType{ - ID: s.ID, - Name: s.Name, - Description: s.Description, - Cores: s.Cores, - Memory: s.Memory, - Disk: s.Disk, - StorageType: StorageType(s.StorageType), - CPUType: CPUType(s.CPUType), - Architecture: Architecture(s.Architecture), - IncludedTraffic: s.IncludedTraffic, - DeprecatableResource: DeprecatableResource{ - DeprecationFromSchema(s.Deprecation), - }, - } - for _, price := range s.Prices { - st.Pricings = append(st.Pricings, ServerTypeLocationPricing{ - Location: &Location{Name: price.Location}, - Hourly: Price{ - Net: price.PriceHourly.Net, - Gross: price.PriceHourly.Gross, - }, - Monthly: Price{ - Net: price.PriceMonthly.Net, - Gross: price.PriceMonthly.Gross, - }, - }) - } - - return st + return c.ServerTypeFromSchema(s) +} + +// SchemaFromServerType converts a ServerType to a schema.ServerType. +func SchemaFromServerType(s *ServerType) schema.ServerType { + return c.SchemaFromServerType(s) } // SSHKeyFromSchema converts a schema.SSHKey to a SSHKey. func SSHKeyFromSchema(s schema.SSHKey) *SSHKey { - sshKey := &SSHKey{ - ID: s.ID, - Name: s.Name, - Fingerprint: s.Fingerprint, - PublicKey: s.PublicKey, - Created: s.Created, - } - sshKey.Labels = map[string]string{} - for key, value := range s.Labels { - sshKey.Labels[key] = value - } - return sshKey + return c.SSHKeyFromSchema(s) +} + +// SchemaFromSSHKey converts a SSHKey to a schema.SSHKey. +func SchemaFromSSHKey(s *SSHKey) schema.SSHKey { + return c.SchemaFromSSHKey(s) } // ImageFromSchema converts a schema.Image to an Image. func ImageFromSchema(s schema.Image) *Image { - i := &Image{ - ID: s.ID, - Type: ImageType(s.Type), - Status: ImageStatus(s.Status), - Description: s.Description, - DiskSize: s.DiskSize, - Created: s.Created, - RapidDeploy: s.RapidDeploy, - OSFlavor: s.OSFlavor, - Architecture: Architecture(s.Architecture), - Protection: ImageProtection{ - Delete: s.Protection.Delete, - }, - Deprecated: s.Deprecated, - Deleted: s.Deleted, - } - if s.Name != nil { - i.Name = *s.Name - } - if s.ImageSize != nil { - i.ImageSize = *s.ImageSize - } - if s.OSVersion != nil { - i.OSVersion = *s.OSVersion - } - if s.CreatedFrom != nil { - i.CreatedFrom = &Server{ - ID: s.CreatedFrom.ID, - Name: s.CreatedFrom.Name, - } - } - if s.BoundTo != nil { - i.BoundTo = &Server{ - ID: *s.BoundTo, - } - } - i.Labels = map[string]string{} - for key, value := range s.Labels { - i.Labels[key] = value - } - return i + return c.ImageFromSchema(s) +} + +// SchemaFromImage converts an Image to a schema.Image. +func SchemaFromImage(i *Image) schema.Image { + return c.SchemaFromImage(i) } // VolumeFromSchema converts a schema.Volume to a Volume. func VolumeFromSchema(s schema.Volume) *Volume { - v := &Volume{ - ID: s.ID, - Name: s.Name, - Location: LocationFromSchema(s.Location), - Size: s.Size, - Status: VolumeStatus(s.Status), - LinuxDevice: s.LinuxDevice, - Protection: VolumeProtection{ - Delete: s.Protection.Delete, - }, - Created: s.Created, - } - if s.Server != nil { - v.Server = &Server{ID: *s.Server} - } - v.Labels = map[string]string{} - for key, value := range s.Labels { - v.Labels[key] = value - } - return v + return c.VolumeFromSchema(s) +} + +// SchemaFromVolume converts a Volume to a schema.Volume. +func SchemaFromVolume(v *Volume) schema.Volume { + return c.SchemaFromVolume(v) } // NetworkFromSchema converts a schema.Network to a Network. func NetworkFromSchema(s schema.Network) *Network { - n := &Network{ - ID: s.ID, - Name: s.Name, - Created: s.Created, - Protection: NetworkProtection{ - Delete: s.Protection.Delete, - }, - Labels: map[string]string{}, - ExposeRoutesToVSwitch: s.ExposeRoutesToVSwitch, - } - - _, n.IPRange, _ = net.ParseCIDR(s.IPRange) - - for _, subnet := range s.Subnets { - n.Subnets = append(n.Subnets, NetworkSubnetFromSchema(subnet)) - } - for _, route := range s.Routes { - n.Routes = append(n.Routes, NetworkRouteFromSchema(route)) - } - for _, serverID := range s.Servers { - n.Servers = append(n.Servers, &Server{ID: serverID}) - } - for key, value := range s.Labels { - n.Labels[key] = value - } - - return n + return c.NetworkFromSchema(s) +} + +// SchemaFromNetwork converts a Network to a schema.Network. +func SchemaFromNetwork(n *Network) schema.Network { + return c.SchemaFromNetwork(n) } // NetworkSubnetFromSchema converts a schema.NetworkSubnet to a NetworkSubnet. func NetworkSubnetFromSchema(s schema.NetworkSubnet) NetworkSubnet { - sn := NetworkSubnet{ - Type: NetworkSubnetType(s.Type), - NetworkZone: NetworkZone(s.NetworkZone), - Gateway: net.ParseIP(s.Gateway), - VSwitchID: s.VSwitchID, - } - _, sn.IPRange, _ = net.ParseCIDR(s.IPRange) - return sn + return c.NetworkSubnetFromSchema(s) +} + +// SchemaFromNetworkSubnet converts a NetworkSubnet to a schema.NetworkSubnet. +func SchemaFromNetworkSubnet(n NetworkSubnet) schema.NetworkSubnet { + return c.SchemaFromNetworkSubnet(n) } // NetworkRouteFromSchema converts a schema.NetworkRoute to a NetworkRoute. func NetworkRouteFromSchema(s schema.NetworkRoute) NetworkRoute { - r := NetworkRoute{ - Gateway: net.ParseIP(s.Gateway), - } - _, r.Destination, _ = net.ParseCIDR(s.Destination) - return r + return c.NetworkRouteFromSchema(s) +} + +// SchemaFromNetworkRoute converts a NetworkRoute to a schema.NetworkRoute. +func SchemaFromNetworkRoute(n NetworkRoute) schema.NetworkRoute { + return c.SchemaFromNetworkRoute(n) } // LoadBalancerTypeFromSchema converts a schema.LoadBalancerType to a LoadBalancerType. func LoadBalancerTypeFromSchema(s schema.LoadBalancerType) *LoadBalancerType { - lt := &LoadBalancerType{ - ID: s.ID, - Name: s.Name, - Description: s.Description, - MaxConnections: s.MaxConnections, - MaxServices: s.MaxServices, - MaxTargets: s.MaxTargets, - MaxAssignedCertificates: s.MaxAssignedCertificates, - } - for _, price := range s.Prices { - lt.Pricings = append(lt.Pricings, LoadBalancerTypeLocationPricing{ - Location: &Location{Name: price.Location}, - Hourly: Price{ - Net: price.PriceHourly.Net, - Gross: price.PriceHourly.Gross, - }, - Monthly: Price{ - Net: price.PriceMonthly.Net, - Gross: price.PriceMonthly.Gross, - }, - }) - } - return lt + return c.LoadBalancerTypeFromSchema(s) +} + +// SchemaFromLoadBalancerType converts a LoadBalancerType to a schema.LoadBalancerType. +func SchemaFromLoadBalancerType(l *LoadBalancerType) schema.LoadBalancerType { + return c.SchemaFromLoadBalancerType(l) } // LoadBalancerFromSchema converts a schema.LoadBalancer to a LoadBalancer. func LoadBalancerFromSchema(s schema.LoadBalancer) *LoadBalancer { - l := &LoadBalancer{ - ID: s.ID, - Name: s.Name, - PublicNet: LoadBalancerPublicNet{ - Enabled: s.PublicNet.Enabled, - IPv4: LoadBalancerPublicNetIPv4{ - IP: net.ParseIP(s.PublicNet.IPv4.IP), - DNSPtr: s.PublicNet.IPv4.DNSPtr, - }, - IPv6: LoadBalancerPublicNetIPv6{ - IP: net.ParseIP(s.PublicNet.IPv6.IP), - DNSPtr: s.PublicNet.IPv6.DNSPtr, - }, - }, - Location: LocationFromSchema(s.Location), - LoadBalancerType: LoadBalancerTypeFromSchema(s.LoadBalancerType), - Algorithm: LoadBalancerAlgorithm{Type: LoadBalancerAlgorithmType(s.Algorithm.Type)}, - Protection: LoadBalancerProtection{ - Delete: s.Protection.Delete, - }, - Labels: map[string]string{}, - Created: s.Created, - IncludedTraffic: s.IncludedTraffic, - } - for _, privateNet := range s.PrivateNet { - l.PrivateNet = append(l.PrivateNet, LoadBalancerPrivateNet{ - Network: &Network{ID: privateNet.Network}, - IP: net.ParseIP(privateNet.IP), - }) - } - if s.OutgoingTraffic != nil { - l.OutgoingTraffic = *s.OutgoingTraffic - } - if s.IngoingTraffic != nil { - l.IngoingTraffic = *s.IngoingTraffic - } - for _, service := range s.Services { - l.Services = append(l.Services, LoadBalancerServiceFromSchema(service)) - } - for _, target := range s.Targets { - l.Targets = append(l.Targets, LoadBalancerTargetFromSchema(target)) - } - for key, value := range s.Labels { - l.Labels[key] = value - } - return l + return c.LoadBalancerFromSchema(s) +} + +// SchemaFromLoadBalancer converts a LoadBalancer to a schema.LoadBalancer. +func SchemaFromLoadBalancer(l *LoadBalancer) schema.LoadBalancer { + return c.SchemaFromLoadBalancer(l) } // LoadBalancerServiceFromSchema converts a schema.LoadBalancerService to a LoadBalancerService. func LoadBalancerServiceFromSchema(s schema.LoadBalancerService) LoadBalancerService { - ls := LoadBalancerService{ - Protocol: LoadBalancerServiceProtocol(s.Protocol), - ListenPort: s.ListenPort, - DestinationPort: s.DestinationPort, - Proxyprotocol: s.Proxyprotocol, - HealthCheck: LoadBalancerServiceHealthCheckFromSchema(s.HealthCheck), - } - if s.HTTP != nil { - ls.HTTP = LoadBalancerServiceHTTP{ - CookieName: s.HTTP.CookieName, - CookieLifetime: time.Duration(s.HTTP.CookieLifetime) * time.Second, - RedirectHTTP: s.HTTP.RedirectHTTP, - StickySessions: s.HTTP.StickySessions, - } - for _, certificateID := range s.HTTP.Certificates { - ls.HTTP.Certificates = append(ls.HTTP.Certificates, &Certificate{ID: certificateID}) - } - } - return ls + return c.LoadBalancerServiceFromSchema(s) +} + +// SchemaFromLoadBalancerService converts a LoadBalancerService to a schema.LoadBalancerService. +func SchemaFromLoadBalancerService(l LoadBalancerService) schema.LoadBalancerService { + return c.SchemaFromLoadBalancerService(l) } // LoadBalancerServiceHealthCheckFromSchema converts a schema.LoadBalancerServiceHealthCheck to a LoadBalancerServiceHealthCheck. func LoadBalancerServiceHealthCheckFromSchema(s *schema.LoadBalancerServiceHealthCheck) LoadBalancerServiceHealthCheck { - lsh := LoadBalancerServiceHealthCheck{ - Protocol: LoadBalancerServiceProtocol(s.Protocol), - Port: s.Port, - Interval: time.Duration(s.Interval) * time.Second, - Retries: s.Retries, - Timeout: time.Duration(s.Timeout) * time.Second, - } - if s.HTTP != nil { - lsh.HTTP = &LoadBalancerServiceHealthCheckHTTP{ - Domain: s.HTTP.Domain, - Path: s.HTTP.Path, - Response: s.HTTP.Response, - StatusCodes: s.HTTP.StatusCodes, - TLS: s.HTTP.TLS, - } - } - return lsh + return c.LoadBalancerServiceHealthCheckFromSchema(s) +} + +// SchemaFromLoadBalancerServiceHealthCheck converts a LoadBalancerServiceHealthCheck to a schema.LoadBalancerServiceHealthCheck. +func SchemaFromLoadBalancerServiceHealthCheck(l LoadBalancerServiceHealthCheck) *schema.LoadBalancerServiceHealthCheck { + return c.SchemaFromLoadBalancerServiceHealthCheck(l) } // LoadBalancerTargetFromSchema converts a schema.LoadBalancerTarget to a LoadBalancerTarget. func LoadBalancerTargetFromSchema(s schema.LoadBalancerTarget) LoadBalancerTarget { - lt := LoadBalancerTarget{ - Type: LoadBalancerTargetType(s.Type), - UsePrivateIP: s.UsePrivateIP, - } - if s.Server != nil { - lt.Server = &LoadBalancerTargetServer{ - Server: &Server{ID: s.Server.ID}, - } - } - if s.LabelSelector != nil { - lt.LabelSelector = &LoadBalancerTargetLabelSelector{ - Selector: s.LabelSelector.Selector, - } - } - if s.IP != nil { - lt.IP = &LoadBalancerTargetIP{IP: s.IP.IP} - } - - for _, healthStatus := range s.HealthStatus { - lt.HealthStatus = append(lt.HealthStatus, LoadBalancerTargetHealthStatusFromSchema(healthStatus)) - } - for _, target := range s.Targets { - lt.Targets = append(lt.Targets, LoadBalancerTargetFromSchema(target)) - } - return lt + return c.LoadBalancerTargetFromSchema(s) +} + +// SchemaFromLoadBalancerTarget converts a LoadBalancerTarget to a schema.LoadBalancerTarget. +func SchemaFromLoadBalancerTarget(l LoadBalancerTarget) schema.LoadBalancerTarget { + return c.SchemaFromLoadBalancerTarget(l) } // LoadBalancerTargetHealthStatusFromSchema converts a schema.LoadBalancerTarget to a LoadBalancerTarget. func LoadBalancerTargetHealthStatusFromSchema(s schema.LoadBalancerTargetHealthStatus) LoadBalancerTargetHealthStatus { - return LoadBalancerTargetHealthStatus{ - ListenPort: s.ListenPort, - Status: LoadBalancerTargetHealthStatusStatus(s.Status), - } + return c.LoadBalancerTargetHealthStatusFromSchema(s) +} + +// SchemaFromLoadBalancerTargetHealthStatus converts a LoadBalancerTarget to a schema.LoadBalancerTarget. +func SchemaFromLoadBalancerTargetHealthStatus(l LoadBalancerTargetHealthStatus) schema.LoadBalancerTargetHealthStatus { + return c.SchemaFromLoadBalancerTargetHealthStatus(l) } // CertificateFromSchema converts a schema.Certificate to a Certificate. func CertificateFromSchema(s schema.Certificate) *Certificate { - c := &Certificate{ - ID: s.ID, - Name: s.Name, - Type: CertificateType(s.Type), - Certificate: s.Certificate, - Created: s.Created, - NotValidBefore: s.NotValidBefore, - NotValidAfter: s.NotValidAfter, - DomainNames: s.DomainNames, - Fingerprint: s.Fingerprint, - } - if s.Status != nil { - c.Status = &CertificateStatus{ - Issuance: CertificateStatusType(s.Status.Issuance), - Renewal: CertificateStatusType(s.Status.Renewal), - } - if s.Status.Error != nil { - certErr := ErrorFromSchema(*s.Status.Error) - c.Status.Error = &certErr - } - } - if len(s.Labels) > 0 { - c.Labels = s.Labels - } - if len(s.UsedBy) > 0 { - c.UsedBy = make([]CertificateUsedByRef, len(s.UsedBy)) - for i, ref := range s.UsedBy { - c.UsedBy[i] = CertificateUsedByRef{ID: ref.ID, Type: CertificateUsedByRefType(ref.Type)} - } - } - - return c + return c.CertificateFromSchema(s) +} + +// SchemaFromCertificate converts a Certificate to a schema.Certificate. +func SchemaFromCertificate(ct *Certificate) schema.Certificate { + return c.SchemaFromCertificate(ct) } // PaginationFromSchema converts a schema.MetaPagination to a Pagination. func PaginationFromSchema(s schema.MetaPagination) Pagination { - return Pagination{ - Page: s.Page, - PerPage: s.PerPage, - PreviousPage: s.PreviousPage, - NextPage: s.NextPage, - LastPage: s.LastPage, - TotalEntries: s.TotalEntries, - } + return c.PaginationFromSchema(s) +} + +// SchemaFromPagination converts a Pagination to a schema.MetaPagination. +func SchemaFromPagination(p Pagination) schema.MetaPagination { + return c.SchemaFromPagination(p) } // ErrorFromSchema converts a schema.Error to an Error. func ErrorFromSchema(s schema.Error) Error { - e := Error{ - Code: ErrorCode(s.Code), - Message: s.Message, - } - - if d, ok := s.Details.(schema.ErrorDetailsInvalidInput); ok { - details := ErrorDetailsInvalidInput{ - Fields: []ErrorDetailsInvalidInputField{}, - } - for _, field := range d.Fields { - details.Fields = append(details.Fields, ErrorDetailsInvalidInputField{ - Name: field.Name, - Messages: field.Messages, - }) - } - e.Details = details - } - return e + return c.ErrorFromSchema(s) +} + +// SchemaFromError converts an Error to a schema.Error. +func SchemaFromError(e Error) schema.Error { + return c.SchemaFromError(e) } // PricingFromSchema converts a schema.Pricing to a Pricing. func PricingFromSchema(s schema.Pricing) Pricing { - p := Pricing{ - Image: ImagePricing{ - PerGBMonth: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: s.Image.PricePerGBMonth.Net, - Gross: s.Image.PricePerGBMonth.Gross, - }, - }, - FloatingIP: FloatingIPPricing{ - Monthly: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: s.FloatingIP.PriceMonthly.Net, - Gross: s.FloatingIP.PriceMonthly.Gross, - }, - }, - Traffic: TrafficPricing{ - PerTB: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: s.Traffic.PricePerTB.Net, - Gross: s.Traffic.PricePerTB.Gross, - }, - }, - ServerBackup: ServerBackupPricing{ - Percentage: s.ServerBackup.Percentage, - }, - Volume: VolumePricing{ - PerGBMonthly: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: s.Volume.PricePerGBPerMonth.Net, - Gross: s.Volume.PricePerGBPerMonth.Gross, - }, - }, - } - for _, floatingIPType := range s.FloatingIPs { - var pricings []FloatingIPTypeLocationPricing - for _, price := range floatingIPType.Prices { - p := FloatingIPTypeLocationPricing{ - Location: &Location{Name: price.Location}, - Monthly: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: price.PriceMonthly.Net, - Gross: price.PriceMonthly.Gross, - }, - } - pricings = append(pricings, p) - } - p.FloatingIPs = append(p.FloatingIPs, FloatingIPTypePricing{Type: FloatingIPType(floatingIPType.Type), Pricings: pricings}) - } - for _, primaryIPType := range s.PrimaryIPs { - var pricings []PrimaryIPTypePricing - for _, price := range primaryIPType.Prices { - p := PrimaryIPTypePricing{ - Location: price.Location, - Monthly: PrimaryIPPrice{ - Net: price.PriceMonthly.Net, - Gross: price.PriceMonthly.Gross, - }, - Hourly: PrimaryIPPrice{ - Net: price.PriceHourly.Net, - Gross: price.PriceHourly.Gross, - }, - } - pricings = append(pricings, p) - } - p.PrimaryIPs = append(p.PrimaryIPs, PrimaryIPPricing{Type: primaryIPType.Type, Pricings: pricings}) - } - for _, serverType := range s.ServerTypes { - var pricings []ServerTypeLocationPricing - for _, price := range serverType.Prices { - pricings = append(pricings, ServerTypeLocationPricing{ - Location: &Location{Name: price.Location}, - Hourly: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: price.PriceHourly.Net, - Gross: price.PriceHourly.Gross, - }, - Monthly: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: price.PriceMonthly.Net, - Gross: price.PriceMonthly.Gross, - }, - }) - } - p.ServerTypes = append(p.ServerTypes, ServerTypePricing{ - ServerType: &ServerType{ - ID: serverType.ID, - Name: serverType.Name, - }, - Pricings: pricings, - }) - } - for _, loadBalancerType := range s.LoadBalancerTypes { - var pricings []LoadBalancerTypeLocationPricing - for _, price := range loadBalancerType.Prices { - pricings = append(pricings, LoadBalancerTypeLocationPricing{ - Location: &Location{Name: price.Location}, - Hourly: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: price.PriceHourly.Net, - Gross: price.PriceHourly.Gross, - }, - Monthly: Price{ - Currency: s.Currency, - VATRate: s.VATRate, - Net: price.PriceMonthly.Net, - Gross: price.PriceMonthly.Gross, - }, - }) - } - p.LoadBalancerTypes = append(p.LoadBalancerTypes, LoadBalancerTypePricing{ - LoadBalancerType: &LoadBalancerType{ - ID: loadBalancerType.ID, - Name: loadBalancerType.Name, - }, - Pricings: pricings, - }) - } - return p + return c.PricingFromSchema(s) +} + +// SchemaFromPricing converts a Pricing to a schema.Pricing. +func SchemaFromPricing(p Pricing) schema.Pricing { + return c.SchemaFromPricing(p) } // FirewallFromSchema converts a schema.Firewall to a Firewall. func FirewallFromSchema(s schema.Firewall) *Firewall { - f := &Firewall{ - ID: s.ID, - Name: s.Name, - Labels: map[string]string{}, - Created: s.Created, - } - for key, value := range s.Labels { - f.Labels[key] = value - } - for _, res := range s.AppliedTo { - r := FirewallResource{Type: FirewallResourceType(res.Type)} - switch r.Type { - case FirewallResourceTypeLabelSelector: - r.LabelSelector = &FirewallResourceLabelSelector{Selector: res.LabelSelector.Selector} - case FirewallResourceTypeServer: - r.Server = &FirewallResourceServer{ID: res.Server.ID} - } - f.AppliedTo = append(f.AppliedTo, r) - } - for _, rule := range s.Rules { - sourceIPs := []net.IPNet{} - for _, sourceIP := range rule.SourceIPs { - _, mask, err := net.ParseCIDR(sourceIP) - if err == nil && mask != nil { - sourceIPs = append(sourceIPs, *mask) - } - } - destinationIPs := []net.IPNet{} - for _, destinationIP := range rule.DestinationIPs { - _, mask, err := net.ParseCIDR(destinationIP) - if err == nil && mask != nil { - destinationIPs = append(destinationIPs, *mask) - } - } - f.Rules = append(f.Rules, FirewallRule{ - Direction: FirewallRuleDirection(rule.Direction), - SourceIPs: sourceIPs, - DestinationIPs: destinationIPs, - Protocol: FirewallRuleProtocol(rule.Protocol), - Port: rule.Port, - Description: rule.Description, - }) - } - return f + return c.FirewallFromSchema(s) +} + +// SchemaFromFirewall converts a Firewall to a schema.Firewall. +func SchemaFromFirewall(f *Firewall) schema.Firewall { + return c.SchemaFromFirewall(f) } // PlacementGroupFromSchema converts a schema.PlacementGroup to a PlacementGroup. func PlacementGroupFromSchema(s schema.PlacementGroup) *PlacementGroup { - g := &PlacementGroup{ - ID: s.ID, - Name: s.Name, - Labels: s.Labels, - Created: s.Created, - Servers: s.Servers, - Type: PlacementGroupType(s.Type), - } - return g + return c.PlacementGroupFromSchema(s) +} + +// SchemaFromPlacementGroup converts a PlacementGroup to a schema.PlacementGroup. +func SchemaFromPlacementGroup(p *PlacementGroup) schema.PlacementGroup { + return c.SchemaFromPlacementGroup(p) +} + +// DeprecationFromSchema converts a [schema.DeprecationInfo] to a [DeprecationInfo]. +func DeprecationFromSchema(s *schema.DeprecationInfo) *DeprecationInfo { + return c.DeprecationFromSchema(s) +} + +// SchemaFromDeprecation converts a [DeprecationInfo] to a [schema.DeprecationInfo]. +func SchemaFromDeprecation(d *DeprecationInfo) *schema.DeprecationInfo { + return c.SchemaFromDeprecation(d) } func placementGroupCreateOptsToSchema(opts PlacementGroupCreateOpts) schema.PlacementGroupCreateRequest { - req := schema.PlacementGroupCreateRequest{ - Name: opts.Name, - Type: string(opts.Type), - } - if opts.Labels != nil { - req.Labels = &opts.Labels - } - return req + return c.SchemaFromPlacementGroupCreateOpts(opts) } func loadBalancerCreateOptsToSchema(opts LoadBalancerCreateOpts) schema.LoadBalancerCreateRequest { - req := schema.LoadBalancerCreateRequest{ - Name: opts.Name, - PublicInterface: opts.PublicInterface, - } - if opts.Algorithm != nil { - req.Algorithm = &schema.LoadBalancerCreateRequestAlgorithm{ - Type: string(opts.Algorithm.Type), - } - } - if opts.LoadBalancerType.ID != 0 { - req.LoadBalancerType = opts.LoadBalancerType.ID - } else if opts.LoadBalancerType.Name != "" { - req.LoadBalancerType = opts.LoadBalancerType.Name - } - if opts.Location != nil { - if opts.Location.ID != 0 { - req.Location = Ptr(strconv.FormatInt(opts.Location.ID, 10)) - } else { - req.Location = Ptr(opts.Location.Name) - } - } - if opts.NetworkZone != "" { - req.NetworkZone = Ptr(string(opts.NetworkZone)) - } - if opts.Labels != nil { - req.Labels = &opts.Labels - } - if opts.Network != nil { - req.Network = Ptr(opts.Network.ID) - } - for _, target := range opts.Targets { - schemaTarget := schema.LoadBalancerCreateRequestTarget{ - UsePrivateIP: target.UsePrivateIP, - } - switch target.Type { - case LoadBalancerTargetTypeServer: - schemaTarget.Type = string(LoadBalancerTargetTypeServer) - schemaTarget.Server = &schema.LoadBalancerCreateRequestTargetServer{ID: target.Server.Server.ID} - case LoadBalancerTargetTypeLabelSelector: - schemaTarget.Type = string(LoadBalancerTargetTypeLabelSelector) - schemaTarget.LabelSelector = &schema.LoadBalancerCreateRequestTargetLabelSelector{Selector: target.LabelSelector.Selector} - case LoadBalancerTargetTypeIP: - schemaTarget.Type = string(LoadBalancerTargetTypeIP) - schemaTarget.IP = &schema.LoadBalancerCreateRequestTargetIP{IP: target.IP.IP} - } - req.Targets = append(req.Targets, schemaTarget) - } - for _, service := range opts.Services { - schemaService := schema.LoadBalancerCreateRequestService{ - Protocol: string(service.Protocol), - ListenPort: service.ListenPort, - DestinationPort: service.DestinationPort, - Proxyprotocol: service.Proxyprotocol, - } - if service.HTTP != nil { - schemaService.HTTP = &schema.LoadBalancerCreateRequestServiceHTTP{ - RedirectHTTP: service.HTTP.RedirectHTTP, - StickySessions: service.HTTP.StickySessions, - CookieName: service.HTTP.CookieName, - } - if service.HTTP.CookieLifetime != nil { - if sec := service.HTTP.CookieLifetime.Seconds(); sec != 0 { - schemaService.HTTP.CookieLifetime = Ptr(int(sec)) - } - } - if service.HTTP.Certificates != nil { - certificates := []int64{} - for _, certificate := range service.HTTP.Certificates { - certificates = append(certificates, certificate.ID) - } - schemaService.HTTP.Certificates = &certificates - } - } - if service.HealthCheck != nil { - schemaHealthCheck := &schema.LoadBalancerCreateRequestServiceHealthCheck{ - Protocol: string(service.HealthCheck.Protocol), - Port: service.HealthCheck.Port, - Retries: service.HealthCheck.Retries, - } - if service.HealthCheck.Interval != nil { - schemaHealthCheck.Interval = Ptr(int(service.HealthCheck.Interval.Seconds())) - } - if service.HealthCheck.Timeout != nil { - schemaHealthCheck.Timeout = Ptr(int(service.HealthCheck.Timeout.Seconds())) - } - if service.HealthCheck.HTTP != nil { - schemaHealthCheckHTTP := &schema.LoadBalancerCreateRequestServiceHealthCheckHTTP{ - Domain: service.HealthCheck.HTTP.Domain, - Path: service.HealthCheck.HTTP.Path, - Response: service.HealthCheck.HTTP.Response, - TLS: service.HealthCheck.HTTP.TLS, - } - if service.HealthCheck.HTTP.StatusCodes != nil { - schemaHealthCheckHTTP.StatusCodes = &service.HealthCheck.HTTP.StatusCodes //nolint:gosec // does not result in bug - } - schemaHealthCheck.HTTP = schemaHealthCheckHTTP - } - schemaService.HealthCheck = schemaHealthCheck - } - req.Services = append(req.Services, schemaService) - } - return req + return c.SchemaFromLoadBalancerCreateOpts(opts) } func loadBalancerAddServiceOptsToSchema(opts LoadBalancerAddServiceOpts) schema.LoadBalancerActionAddServiceRequest { - req := schema.LoadBalancerActionAddServiceRequest{ - Protocol: string(opts.Protocol), - ListenPort: opts.ListenPort, - DestinationPort: opts.DestinationPort, - Proxyprotocol: opts.Proxyprotocol, - } - if opts.HTTP != nil { - req.HTTP = &schema.LoadBalancerActionAddServiceRequestHTTP{ - CookieName: opts.HTTP.CookieName, - RedirectHTTP: opts.HTTP.RedirectHTTP, - StickySessions: opts.HTTP.StickySessions, - } - if opts.HTTP.CookieLifetime != nil { - req.HTTP.CookieLifetime = Ptr(int(opts.HTTP.CookieLifetime.Seconds())) - } - if opts.HTTP.Certificates != nil { - certificates := []int64{} - for _, certificate := range opts.HTTP.Certificates { - certificates = append(certificates, certificate.ID) - } - req.HTTP.Certificates = &certificates - } - } - if opts.HealthCheck != nil { - req.HealthCheck = &schema.LoadBalancerActionAddServiceRequestHealthCheck{ - Protocol: string(opts.HealthCheck.Protocol), - Port: opts.HealthCheck.Port, - Retries: opts.HealthCheck.Retries, - } - if opts.HealthCheck.Interval != nil { - req.HealthCheck.Interval = Ptr(int(opts.HealthCheck.Interval.Seconds())) - } - if opts.HealthCheck.Timeout != nil { - req.HealthCheck.Timeout = Ptr(int(opts.HealthCheck.Timeout.Seconds())) - } - if opts.HealthCheck.HTTP != nil { - req.HealthCheck.HTTP = &schema.LoadBalancerActionAddServiceRequestHealthCheckHTTP{ - Domain: opts.HealthCheck.HTTP.Domain, - Path: opts.HealthCheck.HTTP.Path, - Response: opts.HealthCheck.HTTP.Response, - TLS: opts.HealthCheck.HTTP.TLS, - } - if opts.HealthCheck.HTTP.StatusCodes != nil { - req.HealthCheck.HTTP.StatusCodes = &opts.HealthCheck.HTTP.StatusCodes - } - } - } - return req + return c.SchemaFromLoadBalancerAddServiceOpts(opts) } func loadBalancerUpdateServiceOptsToSchema(opts LoadBalancerUpdateServiceOpts) schema.LoadBalancerActionUpdateServiceRequest { - req := schema.LoadBalancerActionUpdateServiceRequest{ - DestinationPort: opts.DestinationPort, - Proxyprotocol: opts.Proxyprotocol, - } - if opts.Protocol != "" { - req.Protocol = Ptr(string(opts.Protocol)) - } - if opts.HTTP != nil { - req.HTTP = &schema.LoadBalancerActionUpdateServiceRequestHTTP{ - CookieName: opts.HTTP.CookieName, - RedirectHTTP: opts.HTTP.RedirectHTTP, - StickySessions: opts.HTTP.StickySessions, - } - if opts.HTTP.CookieLifetime != nil { - req.HTTP.CookieLifetime = Ptr(int(opts.HTTP.CookieLifetime.Seconds())) - } - if opts.HTTP.Certificates != nil { - certificates := []int64{} - for _, certificate := range opts.HTTP.Certificates { - certificates = append(certificates, certificate.ID) - } - req.HTTP.Certificates = &certificates - } - } - if opts.HealthCheck != nil { - req.HealthCheck = &schema.LoadBalancerActionUpdateServiceRequestHealthCheck{ - Port: opts.HealthCheck.Port, - Retries: opts.HealthCheck.Retries, - } - if opts.HealthCheck.Interval != nil { - req.HealthCheck.Interval = Ptr(int(opts.HealthCheck.Interval.Seconds())) - } - if opts.HealthCheck.Timeout != nil { - req.HealthCheck.Timeout = Ptr(int(opts.HealthCheck.Timeout.Seconds())) - } - if opts.HealthCheck.Protocol != "" { - req.HealthCheck.Protocol = Ptr(string(opts.HealthCheck.Protocol)) - } - if opts.HealthCheck.HTTP != nil { - req.HealthCheck.HTTP = &schema.LoadBalancerActionUpdateServiceRequestHealthCheckHTTP{ - Domain: opts.HealthCheck.HTTP.Domain, - Path: opts.HealthCheck.HTTP.Path, - Response: opts.HealthCheck.HTTP.Response, - TLS: opts.HealthCheck.HTTP.TLS, - } - if opts.HealthCheck.HTTP.StatusCodes != nil { - req.HealthCheck.HTTP.StatusCodes = &opts.HealthCheck.HTTP.StatusCodes - } - } - } - return req + return c.SchemaFromLoadBalancerUpdateServiceOpts(opts) } func firewallCreateOptsToSchema(opts FirewallCreateOpts) schema.FirewallCreateRequest { - req := schema.FirewallCreateRequest{ - Name: opts.Name, - } - if opts.Labels != nil { - req.Labels = &opts.Labels - } - for _, rule := range opts.Rules { - schemaRule := schema.FirewallRule{ - Direction: string(rule.Direction), - Protocol: string(rule.Protocol), - Port: rule.Port, - Description: rule.Description, - } - switch rule.Direction { - case FirewallRuleDirectionOut: - schemaRule.DestinationIPs = make([]string, len(rule.DestinationIPs)) - for i, destinationIP := range rule.DestinationIPs { - schemaRule.DestinationIPs[i] = destinationIP.String() - } - case FirewallRuleDirectionIn: - schemaRule.SourceIPs = make([]string, len(rule.SourceIPs)) - for i, sourceIP := range rule.SourceIPs { - schemaRule.SourceIPs[i] = sourceIP.String() - } - } - req.Rules = append(req.Rules, schemaRule) - } - for _, res := range opts.ApplyTo { - schemaFirewallResource := schema.FirewallResource{ - Type: string(res.Type), - } - switch res.Type { - case FirewallResourceTypeServer: - schemaFirewallResource.Server = &schema.FirewallResourceServer{ - ID: res.Server.ID, - } - case FirewallResourceTypeLabelSelector: - schemaFirewallResource.LabelSelector = &schema.FirewallResourceLabelSelector{Selector: res.LabelSelector.Selector} - } - - req.ApplyTo = append(req.ApplyTo, schemaFirewallResource) - } - return req + return c.SchemaFromFirewallCreateOpts(opts) } func firewallSetRulesOptsToSchema(opts FirewallSetRulesOpts) schema.FirewallActionSetRulesRequest { - req := schema.FirewallActionSetRulesRequest{Rules: []schema.FirewallRule{}} - for _, rule := range opts.Rules { - schemaRule := schema.FirewallRule{ - Direction: string(rule.Direction), - Protocol: string(rule.Protocol), - Port: rule.Port, - Description: rule.Description, - } - switch rule.Direction { - case FirewallRuleDirectionOut: - schemaRule.DestinationIPs = make([]string, len(rule.DestinationIPs)) - for i, destinationIP := range rule.DestinationIPs { - schemaRule.DestinationIPs[i] = destinationIP.String() - } - case FirewallRuleDirectionIn: - schemaRule.SourceIPs = make([]string, len(rule.SourceIPs)) - for i, sourceIP := range rule.SourceIPs { - schemaRule.SourceIPs[i] = sourceIP.String() - } - } - req.Rules = append(req.Rules, schemaRule) - } - return req + return c.SchemaFromFirewallSetRulesOpts(opts) } func firewallResourceToSchema(resource FirewallResource) schema.FirewallResource { - s := schema.FirewallResource{ - Type: string(resource.Type), - } - switch resource.Type { - case FirewallResourceTypeLabelSelector: - s.LabelSelector = &schema.FirewallResourceLabelSelector{Selector: resource.LabelSelector.Selector} - case FirewallResourceTypeServer: - s.Server = &schema.FirewallResourceServer{ID: resource.Server.ID} - } - return s + return c.SchemaFromFirewallResource(resource) } func serverMetricsFromSchema(s *schema.ServerGetMetricsResponse) (*ServerMetrics, error) { - ms := ServerMetrics{ - Start: s.Metrics.Start, - End: s.Metrics.End, - Step: s.Metrics.Step, - } - - timeSeries := make(map[string][]ServerMetricsValue) - for tsName, v := range s.Metrics.TimeSeries { - vals := make([]ServerMetricsValue, len(v.Values)) - - for i, rawVal := range v.Values { - var val ServerMetricsValue - - tup, ok := rawVal.([]interface{}) - if !ok { - return nil, fmt.Errorf("failed to convert value to tuple: %v", rawVal) - } - if len(tup) != 2 { - return nil, fmt.Errorf("invalid tuple size: %d: %v", len(tup), rawVal) - } - ts, ok := tup[0].(float64) - if !ok { - return nil, fmt.Errorf("convert to float64: %v", tup[0]) - } - val.Timestamp = ts - - v, ok := tup[1].(string) - if !ok { - return nil, fmt.Errorf("not a string: %v", tup[1]) - } - val.Value = v - vals[i] = val - } - - timeSeries[tsName] = vals - } - ms.TimeSeries = timeSeries - - return &ms, nil + return c.ServerMetricsFromSchema(s) } func loadBalancerMetricsFromSchema(s *schema.LoadBalancerGetMetricsResponse) (*LoadBalancerMetrics, error) { - ms := LoadBalancerMetrics{ - Start: s.Metrics.Start, - End: s.Metrics.End, - Step: s.Metrics.Step, - } - - timeSeries := make(map[string][]LoadBalancerMetricsValue) - for tsName, v := range s.Metrics.TimeSeries { - vals := make([]LoadBalancerMetricsValue, len(v.Values)) - - for i, rawVal := range v.Values { - var val LoadBalancerMetricsValue - - tup, ok := rawVal.([]interface{}) - if !ok { - return nil, fmt.Errorf("failed to convert value to tuple: %v", rawVal) - } - if len(tup) != 2 { - return nil, fmt.Errorf("invalid tuple size: %d: %v", len(tup), rawVal) - } - ts, ok := tup[0].(float64) - if !ok { - return nil, fmt.Errorf("convert to float64: %v", tup[0]) - } - val.Timestamp = ts - - v, ok := tup[1].(string) - if !ok { - return nil, fmt.Errorf("not a string: %v", tup[1]) - } - val.Value = v - vals[i] = val - } - - timeSeries[tsName] = vals - } - ms.TimeSeries = timeSeries - - return &ms, nil -} - -// DeprecationFromSchema converts a [schema.DeprecationInfo] to a [DeprecationInfo]. -func DeprecationFromSchema(s *schema.DeprecationInfo) *DeprecationInfo { - if s == nil { - return nil - } - - return &DeprecationInfo{ - Announced: s.Announced, - UnavailableAfter: s.UnavailableAfter, - } + return c.LoadBalancerMetricsFromSchema(s) } diff --git a/hcloud/schema_gen.go b/hcloud/schema_gen.go new file mode 100644 index 000000000..a5d393a7a --- /dev/null +++ b/hcloud/schema_gen.go @@ -0,0 +1,878 @@ +package hcloud + +import ( + "encoding/json" + _ "encoding/json" + "fmt" + "net" + "time" + + "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" +) + +//go:generate sh ../script/generate_schema.sh + +// goverter:converter +// goverter:output:file zz_schema.go +// goverter:output:package github.com/hetznercloud/hcloud-go/v2/hcloud +// goverter:useZeroValueOnPointerInconsistency yes +// goverter:skipCopySameType yes +// goverter:extend ipFromString +// goverter:extend stringFromIP +// goverter:extend ipNetFromString +// goverter:extend stringFromIPNet +// goverter:extend timeToTimePtr +// goverter:extend serverFromInt64 +// goverter:extend int64FromServer +// goverter:extend networkFromInt64 +// goverter:extend int64FromNetwork +// goverter:extend volumeFromInt64 +// goverter:extend int64FromVolume +// goverter:extend certificateFromInt64 +// goverter:extend int64FromCertificate +// goverter:extend locationFromString +// goverter:extend stringFromLocation +// goverter:extend serverTypeFromInt64 +// goverter:extend int64FromServerType +// goverter:extend floatingIPFromInt64 +// goverter:extend int64FromFloatingIP +// goverter:extend mapFromFloatingIPDNSPtrSchema +// goverter:extend floatingIPDNSPtrSchemaFromMap +// goverter:extend mapFromPrimaryIPDNSPtrSchema +// goverter:extend primaryIPDNSPtrSchemaFromMap +// goverter:extend mapFromServerPublicNetIPv6DNSPtrSchema +// goverter:extend serverPublicNetIPv6DNSPtrSchemaFromMap +// goverter:extend firewallStatusFromSchemaServerFirewall +// goverter:extend serverFirewallSchemaFromFirewallStatus +// goverter:extend durationFromIntSeconds +// goverter:extend intSecondsFromDuration +// goverter:extend serverFromImageCreatedFromSchema +// goverter:extend anyFromLoadBalancerType +// goverter:extend serverMetricsTimeSeriesFromSchema +// goverter:extend loadBalancerMetricsTimeSeriesFromSchema +// goverter:extend stringPtrFromLoadBalancerServiceProtocol +// goverter:extend stringPtrFromNetworkZone +// goverter:extend schemaFromLoadBalancerCreateOptsTargetLabelSelector +// goverter:extend schemaFromLoadBalancerCreateOptsTargetServer +// goverter:extend schemaFromLoadBalancerCreateOptsTargetIP +// goverter:extend stringMapToStringMapPtr +type converter interface { + + // goverter:map Error.Code ErrorCode + // goverter:map Error.Message ErrorMessage + ActionFromSchema(schema.Action) *Action + + // goverter:map . Error | schemaActionErrorFromAction + SchemaFromAction(*Action) schema.Action + + ActionsFromSchema([]schema.Action) []*Action + + SchemaFromActions([]*Action) []schema.Action + + // goverter:map . IP | ipFromFloatingIPSchema + // goverter:map . Network | networkFromFloatingIPSchema + FloatingIPFromSchema(schema.FloatingIP) *FloatingIP + + // goverter:map . IP | floatingIPToIPString + SchemaFromFloatingIP(*FloatingIP) schema.FloatingIP + + // goverter:map . IP | ipFromPrimaryIPSchema + // goverter:map . Network | networkFromPrimaryIPSchema + PrimaryIPFromSchema(schema.PrimaryIP) *PrimaryIP + + // goverter:map . IP | primaryIPToIPString + SchemaFromPrimaryIP(*PrimaryIP) schema.PrimaryIP + + ISOFromSchema(schema.ISO) *ISO + + SchemaFromISO(*ISO) schema.ISO + + LocationFromSchema(schema.Location) *Location + + SchemaFromLocation(*Location) schema.Location + + DatacenterFromSchema(schema.Datacenter) *Datacenter + + SchemaFromDatacenter(*Datacenter) schema.Datacenter + + ServerFromSchema(schema.Server) *Server + + // goverter:map OutgoingTraffic OutgoingTraffic | mapZeroUint64ToNil + // goverter:map IngoingTraffic IngoingTraffic | mapZeroUint64ToNil + // goverter:map BackupWindow BackupWindow | mapEmptyStringToNil + SchemaFromServer(*Server) schema.Server + + ServerPublicNetFromSchema(schema.ServerPublicNet) ServerPublicNet + + SchemaFromServerPublicNet(ServerPublicNet) schema.ServerPublicNet + + ServerPublicNetIPv4FromSchema(schema.ServerPublicNetIPv4) ServerPublicNetIPv4 + + SchemaFromServerPublicNetIPv4(ServerPublicNetIPv4) schema.ServerPublicNetIPv4 + + // goverter:map . IP | ipFromServerPublicNetIPv6Schema + // goverter:map . Network | ipNetFromServerPublicNetIPv6Schema + ServerPublicNetIPv6FromSchema(schema.ServerPublicNetIPv6) ServerPublicNetIPv6 + + // goverter:map Network IP + SchemaFromServerPublicNetIPv6(ServerPublicNetIPv6) schema.ServerPublicNetIPv6 + + // goverter:map AliasIPs Aliases + ServerPrivateNetFromSchema(schema.ServerPrivateNet) ServerPrivateNet + + // goverter:map Aliases AliasIPs + SchemaFromServerPrivateNet(ServerPrivateNet) schema.ServerPrivateNet + + // goverter:map Prices Pricings + ServerTypeFromSchema(schema.ServerType) *ServerType + + // goverter:map Pricings Prices + SchemaFromServerType(*ServerType) schema.ServerType + + ImageFromSchema(schema.Image) *Image + + SchemaFromImage(*Image) schema.Image + + // goverter:ignore Currency + // goverter:ignore VATRate + PriceFromSchema(schema.Price) Price + + SSHKeyFromSchema(schema.SSHKey) *SSHKey + + SchemaFromSSHKey(*SSHKey) schema.SSHKey + + VolumeFromSchema(schema.Volume) *Volume + + SchemaFromVolume(*Volume) schema.Volume + + NetworkFromSchema(schema.Network) *Network + + SchemaFromNetwork(*Network) schema.Network + + NetworkSubnetFromSchema(schema.NetworkSubnet) NetworkSubnet + + SchemaFromNetworkSubnet(NetworkSubnet) schema.NetworkSubnet + + NetworkRouteFromSchema(schema.NetworkRoute) NetworkRoute + + SchemaFromNetworkRoute(NetworkRoute) schema.NetworkRoute + + LoadBalancerFromSchema(schema.LoadBalancer) *LoadBalancer + + SchemaFromLoadBalancer(*LoadBalancer) schema.LoadBalancer + + // goverter:map Prices Pricings + LoadBalancerTypeFromSchema(schema.LoadBalancerType) *LoadBalancerType + + // goverter:map Pricings Prices + SchemaFromLoadBalancerType(*LoadBalancerType) schema.LoadBalancerType + + // goverter:map PriceHourly Hourly + // goverter:map PriceMonthly Monthly + LoadBalancerTypeLocationPricingFromSchema(schema.PricingLoadBalancerTypePrice) LoadBalancerTypeLocationPricing + + // goverter:map Hourly PriceHourly + // goverter:map Monthly PriceMonthly + SchemaFromLoadBalancerTypeLocationPricing(LoadBalancerTypeLocationPricing) schema.PricingLoadBalancerTypePrice + + LoadBalancerServiceFromSchema(schema.LoadBalancerService) LoadBalancerService + + SchemaFromLoadBalancerService(LoadBalancerService) schema.LoadBalancerService + + LoadBalancerServiceHealthCheckFromSchema(*schema.LoadBalancerServiceHealthCheck) LoadBalancerServiceHealthCheck + + SchemaFromLoadBalancerServiceHealthCheck(LoadBalancerServiceHealthCheck) *schema.LoadBalancerServiceHealthCheck + + LoadBalancerTargetFromSchema(schema.LoadBalancerTarget) LoadBalancerTarget + + SchemaFromLoadBalancerTarget(LoadBalancerTarget) schema.LoadBalancerTarget + + // goverter:map ID Server + LoadBalancerTargetServerFromSchema(schema.LoadBalancerTargetServer) LoadBalancerTargetServer + + // goverter:map Server ID + SchemaFromLoadBalancerServerTarget(LoadBalancerTargetServer) schema.LoadBalancerTargetServer + + LoadBalancerTargetHealthStatusFromSchema(schema.LoadBalancerTargetHealthStatus) LoadBalancerTargetHealthStatus + + SchemaFromLoadBalancerTargetHealthStatus(LoadBalancerTargetHealthStatus) schema.LoadBalancerTargetHealthStatus + + CertificateFromSchema(schema.Certificate) *Certificate + + SchemaFromCertificate(*Certificate) schema.Certificate + + PaginationFromSchema(schema.MetaPagination) Pagination + + SchemaFromPagination(Pagination) schema.MetaPagination + + // goverter:ignore response + // goverter:ignore DetailsRaw + // goverter:map Details Details | errorDetailsFromSchema + ErrorFromSchema(schema.Error) Error + + // goverter:ignore response + // goverter:map Details Details | schemaFromErrorDetails + // goverter:map Details DetailsRaw | rawSchemaFromErrorDetails + SchemaFromError(Error) schema.Error + + // goverter:map . Image | imagePricingFromSchema + // goverter:map . FloatingIP | floatingIPPricingFromSchema + // goverter:map . FloatingIPs | floatingIPTypePricingFromSchema + // goverter:map . PrimaryIPs | primaryIPPricingFromSchema + // goverter:map . Traffic | trafficPricingFromSchema + // goverter:map . ServerTypes | serverTypePricingFromSchema + // goverter:map . LoadBalancerTypes | loadBalancerTypePricingFromSchema + // goverter:map . Volume | volumePricingFromSchema + PricingFromSchema(schema.Pricing) Pricing + + // goverter:map PriceHourly Hourly + // goverter:map PriceMonthly Monthly + serverTypePricingFromSchema(schema.PricingServerTypePrice) ServerTypeLocationPricing + + // goverter:map Image.PerGBMonth.Currency Currency + // goverter:map Image.PerGBMonth.VATRate VATRate + SchemaFromPricing(Pricing) schema.Pricing + + // goverter:map PerGBMonth PricePerGBMonth + schemaFromImagePricing(ImagePricing) schema.PricingImage + + // goverter:map Monthly PriceMonthly + schemaFromFloatingIPPricing(FloatingIPPricing) schema.PricingFloatingIP + + // goverter:map Pricings Prices + schemaFromFloatingIPTypePricing(FloatingIPTypePricing) schema.PricingFloatingIPType + + // goverter:map Monthly PriceMonthly + schemaFromFloatingIPTypeLocationPricing(FloatingIPTypeLocationPricing) schema.PricingFloatingIPTypePrice + + // goverter:map Pricings Prices + schemaFromPrimaryIPPricing(PrimaryIPPricing) schema.PricingPrimaryIP + + // goverter:map Monthly PriceMonthly + // goverter:map Hourly PriceHourly + schemaFromPrimaryIPTypePricing(PrimaryIPTypePricing) schema.PricingPrimaryIPTypePrice + + // goverter:map PerTB PricePerTB + schemaFromTrafficPricing(TrafficPricing) schema.PricingTraffic + + // goverter:map Pricings Prices + // goverter:map ServerType.ID ID + // goverter:map ServerType.Name Name + schemaFromServerTypePricing(ServerTypePricing) schema.PricingServerType + + // goverter:map Pricings Prices + // goverter:map LoadBalancerType.ID ID + // goverter:map LoadBalancerType.Name Name + schemaFromLoadBalancerTypePricing(LoadBalancerTypePricing) schema.PricingLoadBalancerType + + // goverter:map PerGBMonthly PricePerGBPerMonth + schemaFromVolumePricing(VolumePricing) schema.PricingVolume + + // goverter:map Monthly PriceMonthly + // goverter:map Hourly PriceHourly + schemaFromServerTypeLocationPricing(ServerTypeLocationPricing) schema.PricingServerTypePrice + + FirewallFromSchema(schema.Firewall) *Firewall + + SchemaFromFirewall(*Firewall) schema.Firewall + + PlacementGroupFromSchema(schema.PlacementGroup) *PlacementGroup + + SchemaFromPlacementGroup(*PlacementGroup) schema.PlacementGroup + + SchemaFromPlacementGroupCreateOpts(PlacementGroupCreateOpts) schema.PlacementGroupCreateRequest + + SchemaFromLoadBalancerCreateOpts(LoadBalancerCreateOpts) schema.LoadBalancerCreateRequest + + // goverter:map Server.ID ID + SchemaFromLoadBalancerCreateOptsTargetServer(LoadBalancerCreateOptsTargetServer) schema.LoadBalancerCreateRequestTargetServer + + SchemaFromLoadBalancerAddServiceOpts(LoadBalancerAddServiceOpts) schema.LoadBalancerActionAddServiceRequest + + // goverter:ignore ListenPort + SchemaFromLoadBalancerUpdateServiceOpts(LoadBalancerUpdateServiceOpts) schema.LoadBalancerActionUpdateServiceRequest + + SchemaFromFirewallCreateOpts(FirewallCreateOpts) schema.FirewallCreateRequest + + SchemaFromFirewallSetRulesOpts(FirewallSetRulesOpts) schema.FirewallActionSetRulesRequest + + SchemaFromFirewallResource(FirewallResource) schema.FirewallResource + + // goverter:autoMap Metrics + ServerMetricsFromSchema(*schema.ServerGetMetricsResponse) (*ServerMetrics, error) + + // goverter:autoMap Metrics + LoadBalancerMetricsFromSchema(*schema.LoadBalancerGetMetricsResponse) (*LoadBalancerMetrics, error) + + DeprecationFromSchema(*schema.DeprecationInfo) *DeprecationInfo + + SchemaFromDeprecation(*DeprecationInfo) *schema.DeprecationInfo +} + +func schemaActionErrorFromAction(a Action) *schema.ActionError { + if a.ErrorCode != "" && a.ErrorMessage != "" { + return &schema.ActionError{ + Code: a.ErrorCode, + Message: a.ErrorMessage, + } + } + return nil +} + +func ipFromFloatingIPSchema(s schema.FloatingIP) net.IP { + if s.Type == string(FloatingIPTypeIPv4) { + return net.ParseIP(s.IP) + } + ip, _, _ := net.ParseCIDR(s.IP) + return ip +} + +func networkFromFloatingIPSchema(s schema.FloatingIP) *net.IPNet { + if s.Type == string(FloatingIPTypeIPv4) { + return nil + } + _, n, _ := net.ParseCIDR(s.IP) + return n +} + +func ipFromPrimaryIPSchema(s schema.PrimaryIP) net.IP { + if s.Type == string(FloatingIPTypeIPv4) { + return net.ParseIP(s.IP) + } + ip, _, _ := net.ParseCIDR(s.IP) + return ip +} + +func networkFromPrimaryIPSchema(s schema.PrimaryIP) *net.IPNet { + if s.Type == string(FloatingIPTypeIPv4) { + return nil + } + _, n, _ := net.ParseCIDR(s.IP) + return n +} + +func serverFromInt64(id int64) Server { + return Server{ID: id} +} + +func int64FromServer(s Server) int64 { + return s.ID +} + +func networkFromInt64(id int64) Network { + return Network{ID: id} +} + +func int64FromNetwork(network Network) int64 { + return network.ID +} + +func volumeFromInt64(id int64) *Volume { + return &Volume{ID: id} +} + +func int64FromVolume(volume *Volume) int64 { + if volume == nil { + return 0 + } + return volume.ID +} + +func serverTypeFromInt64(id int64) *ServerType { + return &ServerType{ID: id} +} + +func int64FromServerType(s *ServerType) int64 { + if s == nil { + return 0 + } + return s.ID +} + +func certificateFromInt64(id int64) *Certificate { + return &Certificate{ID: id} +} + +func int64FromCertificate(c *Certificate) int64 { + if c == nil { + return 0 + } + return c.ID +} + +func locationFromString(s string) Location { + return Location{Name: s} +} + +func stringFromLocation(l Location) string { + return l.Name +} + +func mapFromFloatingIPDNSPtrSchema(dnsPtr []schema.FloatingIPDNSPtr) map[string]string { + m := make(map[string]string, len(dnsPtr)) + for _, entry := range dnsPtr { + m[entry.IP] = entry.DNSPtr + } + return m +} + +func floatingIPDNSPtrSchemaFromMap(m map[string]string) []schema.FloatingIPDNSPtr { + dnsPtr := make([]schema.FloatingIPDNSPtr, 0, len(m)) + for ip, ptr := range m { + dnsPtr = append(dnsPtr, schema.FloatingIPDNSPtr{ + IP: ip, + DNSPtr: ptr, + }) + } + return dnsPtr +} + +func mapFromPrimaryIPDNSPtrSchema(dnsPtr []schema.PrimaryIPDNSPTR) map[string]string { + m := make(map[string]string, len(dnsPtr)) + for _, entry := range dnsPtr { + m[entry.IP] = entry.DNSPtr + } + return m +} + +func primaryIPDNSPtrSchemaFromMap(m map[string]string) []schema.PrimaryIPDNSPTR { + dnsPtr := make([]schema.PrimaryIPDNSPTR, 0, len(m)) + for ip, ptr := range m { + dnsPtr = append(dnsPtr, schema.PrimaryIPDNSPTR{ + IP: ip, + DNSPtr: ptr, + }) + } + return dnsPtr +} + +func mapFromServerPublicNetIPv6DNSPtrSchema(dnsPtr []schema.ServerPublicNetIPv6DNSPtr) map[string]string { + m := make(map[string]string, len(dnsPtr)) + for _, entry := range dnsPtr { + m[entry.IP] = entry.DNSPtr + } + return m +} + +func serverPublicNetIPv6DNSPtrSchemaFromMap(m map[string]string) []schema.ServerPublicNetIPv6DNSPtr { + dnsPtr := make([]schema.ServerPublicNetIPv6DNSPtr, 0, len(m)) + for ip, ptr := range m { + dnsPtr = append(dnsPtr, schema.ServerPublicNetIPv6DNSPtr{ + IP: ip, + DNSPtr: ptr, + }) + } + return dnsPtr +} + +func floatingIPToIPString(ip FloatingIP) string { + if ip.Type == FloatingIPTypeIPv4 { + return ip.IP.String() + } else { + return ip.Network.String() + } +} + +func primaryIPToIPString(ip PrimaryIP) string { + if ip.Type == PrimaryIPTypeIPv4 { + return ip.IP.String() + } else { + return ip.Network.String() + } +} + +func floatingIPFromInt64(id int64) *FloatingIP { + return &FloatingIP{ID: id} +} + +func int64FromFloatingIP(f *FloatingIP) int64 { + if f == nil { + return 0 + } + return f.ID +} + +func firewallStatusFromSchemaServerFirewall(fw schema.ServerFirewall) *ServerFirewallStatus { + return &ServerFirewallStatus{ + Firewall: Firewall{ID: fw.ID}, + Status: FirewallStatus(fw.Status), + } +} + +func serverFirewallSchemaFromFirewallStatus(s *ServerFirewallStatus) schema.ServerFirewall { + return schema.ServerFirewall{ + ID: s.Firewall.ID, + Status: string(s.Status), + } +} + +func ipFromServerPublicNetIPv6Schema(s schema.ServerPublicNetIPv6) net.IP { + ip, _, _ := net.ParseCIDR(s.IP) + return ip +} + +func ipNetFromServerPublicNetIPv6Schema(s schema.ServerPublicNetIPv6) *net.IPNet { + _, n, _ := net.ParseCIDR(s.IP) + return n +} + +func serverFromImageCreatedFromSchema(s schema.ImageCreatedFrom) Server { + return Server{ + ID: s.ID, + Name: s.Name, + } +} + +func ipFromString(s string) net.IP { + return net.ParseIP(s) +} + +func stringFromIP(ip net.IP) string { + if ip == nil { + return "" + } + return ip.String() +} + +func ipNetFromString(s string) net.IPNet { + _, n, _ := net.ParseCIDR(s) + if n == nil { + return net.IPNet{} + } + return *n +} + +func stringFromIPNet(ip net.IPNet) string { + return ip.String() +} + +func timeToTimePtr(t time.Time) *time.Time { + // Some hcloud structs don't use pointers for nullable times, so the zero value + // should be treated as nil. + if t == (time.Time{}) { + return nil + } + return &t +} + +func durationFromIntSeconds(s int) time.Duration { + return time.Duration(s) * time.Second +} + +func intSecondsFromDuration(d time.Duration) int { + return int(d.Seconds()) +} + +func errorDetailsFromSchema(d interface{}) interface{} { + if d, ok := d.(schema.ErrorDetailsInvalidInput); ok { + details := ErrorDetailsInvalidInput{ + Fields: make([]ErrorDetailsInvalidInputField, len(d.Fields)), + } + for i, field := range d.Fields { + details.Fields[i] = ErrorDetailsInvalidInputField{ + Name: field.Name, + Messages: field.Messages, + } + } + return details + } + return nil +} + +func schemaFromErrorDetails(d interface{}) interface{} { + if d, ok := d.(ErrorDetailsInvalidInput); ok { + details := schema.ErrorDetailsInvalidInput{ + Fields: make([]struct { + Name string `json:"name"` + Messages []string `json:"messages"` + }, len(d.Fields)), + } + for i, field := range d.Fields { + details.Fields[i] = struct { + Name string `json:"name"` + Messages []string `json:"messages"` + }{Name: field.Name, Messages: field.Messages} + } + return details + } + return nil +} + +func imagePricingFromSchema(s schema.Pricing) ImagePricing { + return ImagePricing{ + PerGBMonth: Price{ + Net: s.Image.PricePerGBMonth.Net, + Gross: s.Image.PricePerGBMonth.Gross, + Currency: s.Currency, + VATRate: s.VATRate, + }, + } +} + +func floatingIPPricingFromSchema(s schema.Pricing) FloatingIPPricing { + return FloatingIPPricing{ + Monthly: Price{ + Net: s.FloatingIP.PriceMonthly.Net, + Gross: s.FloatingIP.PriceMonthly.Gross, + Currency: s.Currency, + VATRate: s.VATRate, + }, + } +} + +func floatingIPTypePricingFromSchema(s schema.Pricing) []FloatingIPTypePricing { + p := make([]FloatingIPTypePricing, len(s.FloatingIPs)) + for i, floatingIPType := range s.FloatingIPs { + var pricings = make([]FloatingIPTypeLocationPricing, len(floatingIPType.Prices)) + for i, price := range floatingIPType.Prices { + pricings[i] = FloatingIPTypeLocationPricing{ + Location: &Location{Name: price.Location}, + Monthly: Price{ + Currency: s.Currency, + VATRate: s.VATRate, + Net: price.PriceMonthly.Net, + Gross: price.PriceMonthly.Gross, + }, + } + } + p[i] = FloatingIPTypePricing{Type: FloatingIPType(floatingIPType.Type), Pricings: pricings} + } + return p +} + +func primaryIPPricingFromSchema(s schema.Pricing) []PrimaryIPPricing { + p := make([]PrimaryIPPricing, len(s.FloatingIPs)) + for i, primaryIPType := range s.PrimaryIPs { + var pricings = make([]PrimaryIPTypePricing, len(primaryIPType.Prices)) + for i, price := range primaryIPType.Prices { + pricings[i] = PrimaryIPTypePricing{ + Location: price.Location, + Monthly: PrimaryIPPrice{ + Net: price.PriceMonthly.Net, + Gross: price.PriceMonthly.Gross, + }, + Hourly: PrimaryIPPrice{ + Net: price.PriceHourly.Net, + Gross: price.PriceHourly.Gross, + }, + } + } + p[i] = PrimaryIPPricing{Type: primaryIPType.Type, Pricings: pricings} + } + return p +} + +func trafficPricingFromSchema(s schema.Pricing) TrafficPricing { + return TrafficPricing{ + PerTB: Price{ + Net: s.Traffic.PricePerTB.Net, + Gross: s.Traffic.PricePerTB.Gross, + Currency: s.Currency, + VATRate: s.VATRate, + }, + } +} + +func serverTypePricingFromSchema(s schema.Pricing) []ServerTypePricing { + p := make([]ServerTypePricing, len(s.ServerTypes)) + for i, serverType := range s.ServerTypes { + var pricings = make([]ServerTypeLocationPricing, len(serverType.Prices)) + for i, price := range serverType.Prices { + pricings[i] = ServerTypeLocationPricing{ + Location: &Location{Name: price.Location}, + Hourly: Price{ + Currency: s.Currency, + VATRate: s.VATRate, + Net: price.PriceHourly.Net, + Gross: price.PriceHourly.Gross, + }, + Monthly: Price{ + Currency: s.Currency, + VATRate: s.VATRate, + Net: price.PriceMonthly.Net, + Gross: price.PriceMonthly.Gross, + }, + } + } + p[i] = ServerTypePricing{ + ServerType: &ServerType{ + ID: serverType.ID, + Name: serverType.Name, + }, + Pricings: pricings, + } + } + return p +} + +func loadBalancerTypePricingFromSchema(s schema.Pricing) []LoadBalancerTypePricing { + p := make([]LoadBalancerTypePricing, len(s.LoadBalancerTypes)) + for i, loadBalancerType := range s.LoadBalancerTypes { + var pricings = make([]LoadBalancerTypeLocationPricing, len(loadBalancerType.Prices)) + for i, price := range loadBalancerType.Prices { + pricings[i] = LoadBalancerTypeLocationPricing{ + Location: &Location{Name: price.Location}, + Hourly: Price{ + Currency: s.Currency, + VATRate: s.VATRate, + Net: price.PriceHourly.Net, + Gross: price.PriceHourly.Gross, + }, + Monthly: Price{ + Currency: s.Currency, + VATRate: s.VATRate, + Net: price.PriceMonthly.Net, + Gross: price.PriceMonthly.Gross, + }, + } + } + p[i] = LoadBalancerTypePricing{ + LoadBalancerType: &LoadBalancerType{ + ID: loadBalancerType.ID, + Name: loadBalancerType.Name, + }, + Pricings: pricings, + } + } + return p +} + +func volumePricingFromSchema(s schema.Pricing) VolumePricing { + return VolumePricing{ + PerGBMonthly: Price{ + Net: s.Volume.PricePerGBPerMonth.Net, + Gross: s.Volume.PricePerGBPerMonth.Gross, + Currency: s.Currency, + VATRate: s.VATRate, + }, + } +} + +func anyFromLoadBalancerType(t *LoadBalancerType) interface{} { + if t == nil { + return nil + } + if t.ID != 0 { + return t.ID + } + return t.Name +} + +func serverMetricsTimeSeriesFromSchema(s schema.ServerTimeSeriesVals) ([]ServerMetricsValue, error) { + vals := make([]ServerMetricsValue, len(s.Values)) + + for i, rawVal := range s.Values { + var val ServerMetricsValue + + tup, ok := rawVal.([]interface{}) + if !ok { + return nil, fmt.Errorf("failed to convert value to tuple: %v", rawVal) + } + if len(tup) != 2 { + return nil, fmt.Errorf("invalid tuple size: %d: %v", len(tup), rawVal) + } + ts, ok := tup[0].(float64) + if !ok { + return nil, fmt.Errorf("convert to float64: %v", tup[0]) + } + val.Timestamp = ts + + v, ok := tup[1].(string) + if !ok { + return nil, fmt.Errorf("not a string: %v", tup[1]) + } + val.Value = v + vals[i] = val + } + + return vals, nil +} + +func loadBalancerMetricsTimeSeriesFromSchema(s schema.LoadBalancerTimeSeriesVals) ([]LoadBalancerMetricsValue, error) { + vals := make([]LoadBalancerMetricsValue, len(s.Values)) + + for i, rawVal := range s.Values { + var val LoadBalancerMetricsValue + + tup, ok := rawVal.([]interface{}) + if !ok { + return nil, fmt.Errorf("failed to convert value to tuple: %v", rawVal) + } + if len(tup) != 2 { + return nil, fmt.Errorf("invalid tuple size: %d: %v", len(tup), rawVal) + } + ts, ok := tup[0].(float64) + if !ok { + return nil, fmt.Errorf("convert to float64: %v", tup[0]) + } + val.Timestamp = ts + + v, ok := tup[1].(string) + if !ok { + return nil, fmt.Errorf("not a string: %v", tup[1]) + } + val.Value = v + vals[i] = val + } + + return vals, nil +} + +func mapEmptyStringToNil(s string) *string { + if s == "" { + return nil + } + return &s +} + +func stringPtrFromLoadBalancerServiceProtocol(p LoadBalancerServiceProtocol) *string { + return mapEmptyStringToNil(string(p)) +} + +func stringPtrFromNetworkZone(z NetworkZone) *string { + return mapEmptyStringToNil(string(z)) +} + +func mapZeroUint64ToNil(i uint64) *uint64 { + if i == 0 { + return nil + } + return &i +} + +func schemaFromLoadBalancerCreateOptsTargetLabelSelector(l LoadBalancerCreateOptsTargetLabelSelector) *schema.LoadBalancerCreateRequestTargetLabelSelector { + if l.Selector == "" { + return nil + } + return &schema.LoadBalancerCreateRequestTargetLabelSelector{Selector: l.Selector} +} + +func schemaFromLoadBalancerCreateOptsTargetIP(l LoadBalancerCreateOptsTargetIP) *schema.LoadBalancerCreateRequestTargetIP { + if l.IP == "" { + return nil + } + return &schema.LoadBalancerCreateRequestTargetIP{IP: l.IP} +} + +func schemaFromLoadBalancerCreateOptsTargetServer(l LoadBalancerCreateOptsTargetServer) *schema.LoadBalancerCreateRequestTargetServer { + if l.Server == nil { + return nil + } + return &schema.LoadBalancerCreateRequestTargetServer{ID: l.Server.ID} +} + +func stringMapToStringMapPtr(m map[string]string) *map[string]string { + if m == nil { + return nil + } + return &m +} + +func rawSchemaFromErrorDetails(v interface{}) json.RawMessage { + d := schemaFromErrorDetails(v) + if v == nil { + return nil + } + msg, _ := json.Marshal(d) + return msg +} diff --git a/hcloud/schema_test.go b/hcloud/schema_test.go index 070415e7b..71a29bca3 100644 --- a/hcloud/schema_test.go +++ b/hcloud/schema_test.go @@ -11,7 +11,7 @@ import ( "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" ) -func TestActionFromSchema(t *testing.T) { +func TestActionSchema(t *testing.T) { data := []byte(`{ "id": 1, "command": "create_server", @@ -32,48 +32,15 @@ func TestActionFromSchema(t *testing.T) { }`) var s schema.Action - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - action := ActionFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if action.ID != 1 { - t.Errorf("unexpected ID: %v", action.ID) - } - if action.Command != "create_server" { - t.Errorf("unexpected command: %v", action.Command) - } - if action.Status != "success" { - t.Errorf("unexpected status: %v", action.Status) - } - if action.Progress != 100 { - t.Errorf("unexpected progress: %d", action.Progress) - } - if !action.Started.Equal(time.Date(2016, 1, 30, 23, 55, 0, 0, time.UTC)) { - t.Errorf("unexpected started: %v", action.Started) - } - if !action.Finished.Equal(time.Date(2016, 1, 30, 23, 56, 13, 0, time.UTC)) { - t.Errorf("unexpected finished: %v", action.Started) - } - if action.ErrorCode != "action_failed" { - t.Errorf("unexpected error code: %v", action.ErrorCode) - } - if action.ErrorMessage != "Action failed" { - t.Errorf("unexpected error message: %v", action.ErrorMessage) - } - if len(action.Resources) == 1 { - if action.Resources[0].ID != 42 { - t.Errorf("unexpected id in resources[0].ID: %v", action.Resources[0].ID) - } - if action.Resources[0].Type != ActionResourceTypeServer { - t.Errorf("unexpected type in resources[0].Type: %v", action.Resources[0].Type) - } - } else { - t.Errorf("unexpected number of resources") - } + assert.Equal(t, s, SchemaFromAction(ActionFromSchema(s))) + + a := ActionFromSchema(s) + assert.Equal(t, a, ActionFromSchema(SchemaFromAction(a))) } -func TestActionsFromSchema(t *testing.T) { +func TestActionsSchema(t *testing.T) { data := []byte(`[ { "id": 13, @@ -84,17 +51,17 @@ func TestActionsFromSchema(t *testing.T) { "command": "start_server" } ]`) + var s []schema.Action - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - actions := ActionsFromSchema(s) - if len(actions) != 2 || actions[0].ID != 13 || actions[1].ID != 14 { - t.Fatal("unexpected actions") - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromActions(ActionsFromSchema(s))) + + a := ActionsFromSchema(s) + assert.Equal(t, a, ActionsFromSchema(SchemaFromActions(a))) } -func TestFloatingIPFromSchema(t *testing.T) { +func TestFloatingIPSchema(t *testing.T) { t.Run("IPv6", func(t *testing.T) { data := []byte(`{ "id": 4711, @@ -126,47 +93,12 @@ func TestFloatingIPFromSchema(t *testing.T) { }`) var s schema.FloatingIP - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - floatingIP := FloatingIPFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if floatingIP.ID != 4711 { - t.Errorf("unexpected ID: %v", floatingIP.ID) - } - if !floatingIP.Blocked { - t.Errorf("unexpected value for Blocked: %v", floatingIP.Blocked) - } - if floatingIP.Name != "Web Frontend" { - t.Errorf("unexpected name: %v", floatingIP.Name) - } - if floatingIP.Description != "Web Frontend" { - t.Errorf("unexpected description: %v", floatingIP.Description) - } - if floatingIP.IP.String() != "2001:db8::" { - t.Errorf("unexpected IP: %v", floatingIP.IP) - } - if floatingIP.Type != FloatingIPTypeIPv6 { - t.Errorf("unexpected Type: %v", floatingIP.Type) - } - if floatingIP.Server != nil { - t.Errorf("unexpected Server: %v", floatingIP.Server) - } - if floatingIP.DNSPtr == nil || floatingIP.DNSPtrForIP(floatingIP.IP) != "" { - t.Errorf("unexpected DNS ptr: %v", floatingIP.DNSPtr) - } - if floatingIP.HomeLocation == nil || floatingIP.HomeLocation.ID != 1 { - t.Errorf("unexpected home location: %v", floatingIP.HomeLocation) - } - if !floatingIP.Protection.Delete { - t.Errorf("unexpected Protection.Delete: %v", floatingIP.Protection.Delete) - } - if floatingIP.Labels["key"] != "value" || floatingIP.Labels["key2"] != "value2" { - t.Errorf("unexpected Labels: %v", floatingIP.Labels) - } - if !floatingIP.Created.Equal(time.Date(2017, 8, 16, 17, 29, 14, 0, time.UTC)) { - t.Errorf("unexpected created date: %v", floatingIP.Created) - } + assert.Equal(t, s, SchemaFromFloatingIP(FloatingIPFromSchema(s))) + + ip := FloatingIPFromSchema(s) + assert.Equal(t, ip, FloatingIPFromSchema(SchemaFromFloatingIP(ip))) }) t.Run("IPv4", func(t *testing.T) { @@ -193,236 +125,130 @@ func TestFloatingIPFromSchema(t *testing.T) { }`) var s schema.FloatingIP - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - floatingIP := FloatingIPFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if floatingIP.ID != 4711 { - t.Errorf("unexpected ID: %v", floatingIP.ID) - } - if floatingIP.Blocked { - t.Errorf("unexpected value for Blocked: %v", floatingIP.Blocked) - } - if floatingIP.Description != "Web Frontend" { - t.Errorf("unexpected description: %v", floatingIP.Description) - } - if floatingIP.IP.String() != "131.232.99.1" { - t.Errorf("unexpected IP: %v", floatingIP.IP) - } - if floatingIP.Type != FloatingIPTypeIPv4 { - t.Errorf("unexpected type: %v", floatingIP.Type) - } - if floatingIP.Server == nil || floatingIP.Server.ID != 42 { - t.Errorf("unexpected server: %v", floatingIP.Server) - } - if floatingIP.DNSPtr == nil || floatingIP.DNSPtrForIP(floatingIP.IP) != "fip01.example.com" { - t.Errorf("unexpected DNS ptr: %v", floatingIP.DNSPtr) - } - if floatingIP.HomeLocation == nil || floatingIP.HomeLocation.ID != 1 { - t.Errorf("unexpected home location: %v", floatingIP.HomeLocation) - } + assert.Equal(t, s, SchemaFromFloatingIP(FloatingIPFromSchema(s))) + + ip := FloatingIPFromSchema(s) + assert.Equal(t, ip, FloatingIPFromSchema(SchemaFromFloatingIP(ip))) }) } -func TestPrimaryIPFromSchema(t *testing.T) { +func TestPrimaryIPSchema(t *testing.T) { t.Run("IPv6", func(t *testing.T) { data := []byte(`{ - "assignee_id": 17, - "assignee_type": "server", - "auto_delete": true, - "blocked": true, - "created": "2017-08-16T17:29:14+00:00", - "datacenter": { - "description": "Falkenstein DC Park 8", - "id": 42, - "location": { - "city": "Falkenstein", - "country": "DE", - "description": "Falkenstein DC Park 1", - "id": 1, - "latitude": 50.47612, - "longitude": 12.370071, - "name": "fsn1", - "network_zone": "eu-central" + "assignee_id": 17, + "assignee_type": "server", + "auto_delete": true, + "blocked": true, + "created": "2017-08-16T17:29:14+00:00", + "datacenter": { + "description": "Falkenstein DC Park 8", + "id": 42, + "location": { + "city": "Falkenstein", + "country": "DE", + "description": "Falkenstein DC Park 1", + "id": 1, + "latitude": 50.47612, + "longitude": 12.370071, + "name": "fsn1", + "network_zone": "eu-central" + }, + "name": "fsn1-dc8", + "server_types": { + "available": [], + "available_for_migration": [], + "supported": [] + } }, - "name": "fsn1-dc8", - "server_types": { - "available": [], - "available_for_migration": [], - "supported": [] - } - }, - "dns_ptr": [ - { - "dns_ptr": "server.example.com", - "ip": "fe80::" - } - ], - "id": 4711, - "ip": "fe80::/64", - "labels": { - "key": "value", - "key2": "value2" - }, - "name": "Web Frontend", - "protection": { - "delete": true - }, - "type": "ipv6" - }`) + "dns_ptr": [ + { + "dns_ptr": "server.example.com", + "ip": "fe80::" + } + ], + "id": 4711, + "ip": "fe80::/64", + "labels": { + "key": "value", + "key2": "value2" + }, + "name": "Web Frontend", + "protection": { + "delete": true + }, + "type": "ipv6" + }`) var s schema.PrimaryIP - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - primaryIP := PrimaryIPFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if primaryIP.ID != 4711 { - t.Errorf("unexpected ID: %v", primaryIP.ID) - } - if !primaryIP.Blocked { - t.Errorf("unexpected value for Blocked: %v", primaryIP.Blocked) - } - if !primaryIP.AutoDelete { - t.Errorf("unexpected value for AutoDelete: %v", primaryIP.AutoDelete) - } - if primaryIP.Name != "Web Frontend" { - t.Errorf("unexpected name: %v", primaryIP.Name) - } + assert.Equal(t, s, SchemaFromPrimaryIP(PrimaryIPFromSchema(s))) - if primaryIP.IP.String() != "fe80::" { - t.Errorf("unexpected IP: %v", primaryIP.IP) - } - if primaryIP.Type != PrimaryIPTypeIPv6 { - t.Errorf("unexpected Type: %v", primaryIP.Type) - } - if primaryIP.AssigneeType != "server" { - t.Errorf("unexpected AssigneeType: %v", primaryIP.AssigneeType) - } - if primaryIP.AssigneeID != 17 { - t.Errorf("unexpected AssigneeID: %v", primaryIP.AssigneeID) - } - dnsPTR, err := primaryIP.GetDNSPtrForIP(primaryIP.IP) - if err != nil { - t.Fatal(err) - } - if primaryIP.DNSPtr == nil || dnsPTR == "" { - t.Errorf("unexpected DNS ptr: %v", primaryIP.DNSPtr) - } - if primaryIP.Datacenter.Name != "fsn1-dc8" { - t.Errorf("unexpected datacenter: %v", primaryIP.Datacenter) - } - if !primaryIP.Protection.Delete { - t.Errorf("unexpected Protection.Delete: %v", primaryIP.Protection.Delete) - } - if primaryIP.Labels["key"] != "value" || primaryIP.Labels["key2"] != "value2" { - t.Errorf("unexpected Labels: %v", primaryIP.Labels) - } - if !primaryIP.Created.Equal(time.Date(2017, 8, 16, 17, 29, 14, 0, time.UTC)) { - t.Errorf("unexpected created date: %v", primaryIP.Created) - } + ip := PrimaryIPFromSchema(s) + assert.Equal(t, ip, PrimaryIPFromSchema(SchemaFromPrimaryIP(ip))) }) + t.Run("IPv4", func(t *testing.T) { data := []byte(`{ - "assignee_id": 17, - "assignee_type": "server", - "auto_delete": true, - "blocked": true, - "created": "2017-08-16T17:29:14+00:00", - "datacenter": { - "description": "Falkenstein DC Park 8", - "id": 42, - "location": { - "city": "Falkenstein", - "country": "DE", - "description": "Falkenstein DC Park 1", - "id": 1, - "latitude": 50.47612, - "longitude": 12.370071, - "name": "fsn1", - "network_zone": "eu-central" + "assignee_id": 17, + "assignee_type": "server", + "auto_delete": true, + "blocked": true, + "created": "2017-08-16T17:29:14+00:00", + "datacenter": { + "description": "Falkenstein DC Park 8", + "id": 42, + "location": { + "city": "Falkenstein", + "country": "DE", + "description": "Falkenstein DC Park 1", + "id": 1, + "latitude": 50.47612, + "longitude": 12.370071, + "name": "fsn1", + "network_zone": "eu-central" + }, + "name": "fsn1-dc8", + "server_types": { + "available": [], + "available_for_migration": [], + "supported": [] + } }, - "name": "fsn1-dc8", - "server_types": { - "available": [], - "available_for_migration": [], - "supported": [] - } - }, - "dns_ptr": [ - { - "dns_ptr": "server.example.com", - "ip": "127.0.0.1" - } - ], - "id": 4711, - "ip": "127.0.0.1", - "labels": { - "key": "value", - "key2": "value2" - }, - "name": "Web Frontend", - "protection": { - "delete": true - }, - "type": "ipv4" - }`) + "dns_ptr": [ + { + "dns_ptr": "server.example.com", + "ip": "127.0.0.1" + } + ], + "id": 4711, + "ip": "127.0.0.1", + "labels": { + "key": "value", + "key2": "value2" + }, + "name": "Web Frontend", + "protection": { + "delete": true + }, + "type": "ipv4" + }`) var s schema.PrimaryIP if err := json.Unmarshal(data, &s); err != nil { t.Fatal(err) } - primaryIP := PrimaryIPFromSchema(s) - if primaryIP.ID != 4711 { - t.Errorf("unexpected ID: %v", primaryIP.ID) - } - if !primaryIP.Blocked { - t.Errorf("unexpected value for Blocked: %v", primaryIP.Blocked) - } - if !primaryIP.AutoDelete { - t.Errorf("unexpected value for AutoDelete: %v", primaryIP.AutoDelete) - } - if primaryIP.Name != "Web Frontend" { - t.Errorf("unexpected name: %v", primaryIP.Name) - } + assert.Equal(t, s, SchemaFromPrimaryIP(PrimaryIPFromSchema(s))) - if primaryIP.IP.String() != "127.0.0.1" { - t.Errorf("unexpected IP: %v", primaryIP.IP) - } - if primaryIP.Type != PrimaryIPTypeIPv4 { - t.Errorf("unexpected Type: %v", primaryIP.Type) - } - if primaryIP.AssigneeType != "server" { - t.Errorf("unexpected AssigneeType: %v", primaryIP.AssigneeType) - } - if primaryIP.AssigneeID != 17 { - t.Errorf("unexpected AssigneeID: %v", primaryIP.AssigneeID) - } - dnsPTR, err := primaryIP.GetDNSPtrForIP(primaryIP.IP) - if err != nil { - t.Fatal(err) - } - if primaryIP.DNSPtr == nil || dnsPTR == "" { - t.Errorf("unexpected DNS ptr: %v", primaryIP.DNSPtr) - } - if primaryIP.Datacenter.Name != "fsn1-dc8" { - t.Errorf("unexpected datacenter: %v", primaryIP.Datacenter) - } - if !primaryIP.Protection.Delete { - t.Errorf("unexpected Protection.Delete: %v", primaryIP.Protection.Delete) - } - if primaryIP.Labels["key"] != "value" || primaryIP.Labels["key2"] != "value2" { - t.Errorf("unexpected Labels: %v", primaryIP.Labels) - } - if !primaryIP.Created.Equal(time.Date(2017, 8, 16, 17, 29, 14, 0, time.UTC)) { - t.Errorf("unexpected created date: %v", primaryIP.Created) - } + ip := PrimaryIPFromSchema(s) + assert.Equal(t, ip, PrimaryIPFromSchema(SchemaFromPrimaryIP(ip))) }) } -func TestISOFromSchema(t *testing.T) { +func TestISOSchema(t *testing.T) { data := []byte(`{ "id": 4711, "name": "FreeBSD-11.0-RELEASE-amd64-dvd1", @@ -433,33 +259,15 @@ func TestISOFromSchema(t *testing.T) { }`) var s schema.ISO - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromISO(ISOFromSchema(s))) + iso := ISOFromSchema(s) - if iso.ID != 4711 { - t.Errorf("unexpected ID: %v", iso.ID) - } - if iso.Name != "FreeBSD-11.0-RELEASE-amd64-dvd1" { - t.Errorf("unexpected name: %v", iso.Name) - } - if iso.Description != "FreeBSD 11.0 x64" { - t.Errorf("unexpected description: %v", iso.Description) - } - if iso.Type != ISOTypePublic { - t.Errorf("unexpected type: %v", iso.Type) - } - if iso.Architecture == nil { - t.Errorf("unexpected empty architecture") - } else if *iso.Architecture != ArchitectureX86 { - t.Errorf("unexpected architecture: %s", *iso.Architecture) - } - if iso.Deprecated.IsZero() { - t.Errorf("unexpected value for deprecated: %v", iso.Deprecated) - } + assert.Equal(t, iso, ISOFromSchema(SchemaFromISO(iso))) } -func TestDatacenterFromSchema(t *testing.T) { +func TestDatacenterSchema(t *testing.T) { data := []byte(`{ "id": 1, "name": "fsn1-dc8", @@ -491,28 +299,15 @@ func TestDatacenterFromSchema(t *testing.T) { }`) var s schema.Datacenter - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - datacenter := DatacenterFromSchema(s) - if datacenter.ID != 1 { - t.Errorf("unexpected ID: %v", datacenter.ID) - } - if datacenter.Name != "fsn1-dc8" { - t.Errorf("unexpected Name: %v", datacenter.Name) - } - if datacenter.Location == nil || datacenter.Location.ID != 1 { - t.Errorf("unexpected Location: %v", datacenter.Location) - } - if len(datacenter.ServerTypes.Available) != 4 { - t.Errorf("unexpected ServerTypes.Available (should be 4): %v", len(datacenter.ServerTypes.Available)) - } - if len(datacenter.ServerTypes.Supported) != 4 { - t.Errorf("unexpected ServerTypes.Supported length (should be 4): %v", len(datacenter.ServerTypes.Supported)) - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromDatacenter(DatacenterFromSchema(s))) + + dc := DatacenterFromSchema(s) + assert.Equal(t, dc, DatacenterFromSchema(SchemaFromDatacenter(dc))) } -func TestLocationFromSchema(t *testing.T) { +func TestLocationSchema(t *testing.T) { data := []byte(`{ "id": 1, "name": "fsn1", @@ -525,37 +320,15 @@ func TestLocationFromSchema(t *testing.T) { }`) var s schema.Location - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - location := LocationFromSchema(s) - if location.ID != 1 { - t.Errorf("unexpected ID: %v", location.ID) - } - if location.Name != "fsn1" { - t.Errorf("unexpected Name: %v", location.Name) - } - if location.Description != "Falkenstein DC Park 1" { - t.Errorf("unexpected Description: %v", location.Description) - } - if location.Country != "DE" { - t.Errorf("unexpected Country: %v", location.Country) - } - if location.City != "Falkenstein" { - t.Errorf("unexpected City: %v", location.City) - } - if location.Latitude != 50.47612 { - t.Errorf("unexpected Latitude: %v", location.Latitude) - } - if location.Longitude != 12.370071 { - t.Errorf("unexpected Longitude: %v", location.Longitude) - } - if location.NetworkZone != "eu-central" { - t.Errorf("unexpected NetworkZone: %v", location.NetworkZone) - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromLocation(LocationFromSchema(s))) + + l := LocationFromSchema(s) + assert.Equal(t, l, LocationFromSchema(SchemaFromLocation(l))) } -func TestServerFromSchema(t *testing.T) { +func TestServerSchema(t *testing.T) { data := []byte(`{ "id": 1, "name": "server.example.com", @@ -657,89 +430,15 @@ func TestServerFromSchema(t *testing.T) { }`) var s schema.Server - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - server := ServerFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if server.ID != 1 { - t.Errorf("unexpected ID: %v", server.ID) - } - if server.Name != "server.example.com" { - t.Errorf("unexpected name: %v", server.Name) - } - if server.Status != ServerStatusRunning { - t.Errorf("unexpected status: %v", server.Status) - } - if !server.Created.Equal(time.Date(2017, 8, 16, 17, 29, 14, 0, time.UTC)) { - t.Errorf("unexpected created date: %v", server.Created) - } - if !server.PublicNet.IPv4.IsUnspecified() { - t.Errorf("unexpected public net IPv4: %v", server.PublicNet.IPv4) - } - if server.PublicNet.IPv6.IP.String() != "2a01:4f8:1c11:3400::" { - t.Errorf("unexpected public net IPv6 IP: %v", server.PublicNet.IPv6.IP) - } - if server.ServerType.ID != 2 { - t.Errorf("unexpected server type ID: %v", server.ServerType.ID) - } - if server.IncludedTraffic != 654321 { - t.Errorf("unexpected included traffic: %v", server.IncludedTraffic) - } - if server.OutgoingTraffic != 123456 { - t.Errorf("unexpected outgoing traffic: %v", server.OutgoingTraffic) - } - if server.IngoingTraffic != 7891011 { - t.Errorf("unexpected ingoing traffic: %v", server.IngoingTraffic) - } - if server.BackupWindow != "22-02" { - t.Errorf("unexpected backup window: %v", server.BackupWindow) - } - if server.PrimaryDiskSize != 20 { - t.Errorf("unexpected primary disk size: %v", server.PrimaryDiskSize) - } - if !server.RescueEnabled { - t.Errorf("unexpected rescue enabled state: %v", server.RescueEnabled) - } - if server.Image == nil || server.Image.ID != 4711 { - t.Errorf("unexpected Image: %v", server.Image) - } - if server.ISO == nil || server.ISO.ID != 4711 { - t.Errorf("unexpected ISO: %v", server.ISO) - } - if server.Datacenter == nil || server.Datacenter.ID != 1 { - t.Errorf("unexpected Datacenter: %v", server.Datacenter) - } - if !server.Locked { - t.Errorf("unexpected value for Locked: %v", server.Locked) - } - if !server.Protection.Delete { - t.Errorf("unexpected value for Protection.Delete: %v", server.Protection.Delete) - } - if !server.Protection.Rebuild { - t.Errorf("unexpected value for Protection.Rebuild: %v", server.Protection.Rebuild) - } - if server.Labels["key"] != "value" || server.Labels["key2"] != "value2" { - t.Errorf("unexpected Labels: %v", server.Labels) - } - if len(s.Volumes) != 3 { - t.Errorf("unexpected number of volumes: %v", len(s.Volumes)) - } - if s.Volumes[0] != 123 || s.Volumes[1] != 456 || s.Volumes[2] != 789 { - t.Errorf("unexpected volumes: %v", s.Volumes) - } - if len(server.PrivateNet) != 1 { - t.Errorf("unexpected length of PrivateNet: %v", len(server.PrivateNet)) - } - if server.PrivateNet[0].Network.ID != 4711 { - t.Errorf("unexpected first private net: %v", server.PrivateNet[0]) - } - if server.PlacementGroup.ID != 897 { - t.Errorf("unexpected placement group: %d", server.PlacementGroup.ID) - } + assert.Equal(t, s, SchemaFromServer(ServerFromSchema(s))) + + iso := ServerFromSchema(s) + assert.Equal(t, iso, ServerFromSchema(SchemaFromServer(iso))) } -func TestServerFromSchemaNoTraffic(t *testing.T) { +func TestServerSchemaNoTraffic(t *testing.T) { data := []byte(`{ "public_net": { "ipv4": { @@ -763,20 +462,15 @@ func TestServerFromSchemaNoTraffic(t *testing.T) { }`) var s schema.Server - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - server := ServerFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if server.OutgoingTraffic != 0 { - t.Errorf("unexpected outgoing traffic: %v", server.OutgoingTraffic) - } - if server.IngoingTraffic != 0 { - t.Errorf("unexpected ingoing traffic: %v", server.IngoingTraffic) - } + assert.Equal(t, s, SchemaFromServer(ServerFromSchema(s))) + + iso := ServerFromSchema(s) + assert.Equal(t, iso, ServerFromSchema(SchemaFromServer(iso))) } -func TestServerPublicNetFromSchema(t *testing.T) { +func TestServerPublicNetSchema(t *testing.T) { data := []byte(`{ "ipv4": { "id": 1, @@ -800,31 +494,15 @@ func TestServerPublicNetFromSchema(t *testing.T) { }`) var s schema.ServerPublicNet - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - publicNet := ServerPublicNetFromSchema(s) - if publicNet.IPv4.ID != 1 { - t.Errorf("unexpected IPv4 ID: %v", publicNet.IPv4.ID) - } - if publicNet.IPv4.IP.String() != "1.2.3.4" { - t.Errorf("unexpected IPv4 IP: %v", publicNet.IPv4.IP) - } - if publicNet.IPv6.ID != 2 { - t.Errorf("unexpected IPv6 ID: %v", publicNet.IPv6.ID) - } - if publicNet.IPv6.Network.String() != "2a01:4f8:1c19:1403::/64" { - t.Errorf("unexpected IPv6 IP: %v", publicNet.IPv6.IP) - } - if len(publicNet.FloatingIPs) != 1 || publicNet.FloatingIPs[0].ID != 4 { - t.Errorf("unexpected Floating IPs: %v", publicNet.FloatingIPs) - } - if len(publicNet.Firewalls) != 1 || publicNet.Firewalls[0].Firewall.ID != 23 || publicNet.Firewalls[0].Status != FirewallStatusApplied { - t.Errorf("unexpected Firewalls: %v", publicNet.Firewalls) - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromServerPublicNet(ServerPublicNetFromSchema(s))) + + net := ServerPublicNetFromSchema(s) + assert.Equal(t, net, ServerPublicNetFromSchema(SchemaFromServerPublicNet(net))) } -func TestServerPublicNetIPv4FromSchema(t *testing.T) { +func TestServerPublicNetIPv4Schema(t *testing.T) { data := []byte(`{ "ip": "1.2.3.4", "blocked": true, @@ -832,23 +510,15 @@ func TestServerPublicNetIPv4FromSchema(t *testing.T) { }`) var s schema.ServerPublicNetIPv4 - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - ipv4 := ServerPublicNetIPv4FromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if ipv4.IP.String() != "1.2.3.4" { - t.Errorf("unexpected IP: %v", ipv4.IP) - } - if !ipv4.Blocked { - t.Errorf("unexpected blocked state: %v", ipv4.Blocked) - } - if ipv4.DNSPtr != "server.example.com" { - t.Errorf("unexpected DNS ptr: %v", ipv4.DNSPtr) - } + assert.Equal(t, s, SchemaFromServerPublicNetIPv4(ServerPublicNetIPv4FromSchema(s))) + + net := ServerPublicNetIPv4FromSchema(s) + assert.Equal(t, net, ServerPublicNetIPv4FromSchema(SchemaFromServerPublicNetIPv4(net))) } -func TestServerPublicNetIPv6FromSchema(t *testing.T) { +func TestServerPublicNetIPv6Schema(t *testing.T) { data := []byte(`{ "ip": "2a01:4f8:1c11:3400::/64", "blocked": true, @@ -861,23 +531,15 @@ func TestServerPublicNetIPv6FromSchema(t *testing.T) { }`) var s schema.ServerPublicNetIPv6 - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - ipv6 := ServerPublicNetIPv6FromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if ipv6.Network.String() != "2a01:4f8:1c11:3400::/64" { - t.Errorf("unexpected IP: %v", ipv6.IP) - } - if !ipv6.Blocked { - t.Errorf("unexpected blocked state: %v", ipv6.Blocked) - } - if len(ipv6.DNSPtr) != 1 { - t.Errorf("unexpected DNS ptr: %v", ipv6.DNSPtr) - } + assert.Equal(t, s, SchemaFromServerPublicNetIPv6(ServerPublicNetIPv6FromSchema(s))) + + net := ServerPublicNetIPv6FromSchema(s) + assert.Equal(t, net, ServerPublicNetIPv6FromSchema(SchemaFromServerPublicNetIPv6(net))) } -func TestServerPrivateNetFromSchema(t *testing.T) { +func TestServerPrivateNetSchema(t *testing.T) { data := []byte(`{ "network": 4711, "ip": "10.0.1.1", @@ -888,29 +550,15 @@ func TestServerPrivateNetFromSchema(t *testing.T) { }`) var s schema.ServerPrivateNet - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - privateNet := ServerPrivateNetFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if privateNet.Network.ID != 4711 { - t.Errorf("unexpected Network: %v", privateNet.Network) - } - if privateNet.IP.String() != "10.0.1.1" { - t.Errorf("unexpected IP: %v", privateNet.IP) - } - if len(privateNet.Aliases) != 1 { - t.Errorf("unexpected number of alias IPs: %v", len(privateNet.Aliases)) - } - if privateNet.Aliases[0].String() != "10.0.1.2" { - t.Errorf("unexpected alias IP: %v", privateNet.Aliases[0]) - } - if privateNet.MACAddress != "86:00:ff:2a:7d:e1" { - t.Errorf("unexpected mac address: %v", privateNet.MACAddress) - } + assert.Equal(t, s, SchemaFromServerPrivateNet(ServerPrivateNetFromSchema(s))) + + net := ServerPrivateNetFromSchema(s) + assert.Equal(t, net, ServerPrivateNetFromSchema(SchemaFromServerPrivateNet(net))) } -func TestServerTypeFromSchema(t *testing.T) { +func TestServerTypeSchema(t *testing.T) { data := []byte(`{ "id": 1, "name": "cx10", @@ -938,63 +586,15 @@ func TestServerTypeFromSchema(t *testing.T) { }`) var s schema.ServerType - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - serverType := ServerTypeFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if serverType.ID != 1 { - t.Errorf("unexpected ID: %v", serverType.ID) - } - if serverType.Name != "cx10" { - t.Errorf("unexpected name: %q", serverType.Name) - } - if serverType.Description != "description" { - t.Errorf("unexpected description: %q", serverType.Description) - } - if serverType.Cores != 4 { - t.Errorf("unexpected cores: %v", serverType.Cores) - } - if serverType.Memory != 1.0 { - t.Errorf("unexpected memory: %v", serverType.Memory) - } - if serverType.Disk != 20 { - t.Errorf("unexpected disk: %v", serverType.Disk) - } - if serverType.StorageType != StorageTypeLocal { - t.Errorf("unexpected storage type: %q", serverType.StorageType) - } - if serverType.CPUType != CPUTypeShared { - t.Errorf("unexpected cpu type: %q", serverType.CPUType) - } - if serverType.Architecture != ArchitectureX86 { - t.Errorf("unexpected cpu architecture: %q", serverType.Architecture) - } - if serverType.Deprecation != nil { - t.Errorf("unexpected deprecation: %v", serverType.Deprecation) - } - if len(serverType.Pricings) != 1 { - t.Errorf("unexpected number of pricings: %d", len(serverType.Pricings)) - } else { - if serverType.Pricings[0].Location.Name != "fsn1" { - t.Errorf("unexpected location name: %v", serverType.Pricings[0].Location.Name) - } - if serverType.Pricings[0].Hourly.Net != "1" { - t.Errorf("unexpected hourly net price: %v", serverType.Pricings[0].Hourly.Net) - } - if serverType.Pricings[0].Hourly.Gross != "1.19" { - t.Errorf("unexpected hourly gross price: %v", serverType.Pricings[0].Hourly.Gross) - } - if serverType.Pricings[0].Monthly.Net != "1" { - t.Errorf("unexpected monthly net price: %v", serverType.Pricings[0].Monthly.Net) - } - if serverType.Pricings[0].Monthly.Gross != "1.19" { - t.Errorf("unexpected monthly gross price: %v", serverType.Pricings[0].Monthly.Gross) - } - } + assert.Equal(t, s, SchemaFromServerType(ServerTypeFromSchema(s))) + + net := ServerTypeFromSchema(s) + assert.Equal(t, net, ServerTypeFromSchema(SchemaFromServerType(net))) } -func TestSSHKeyFromSchema(t *testing.T) { +func TestSSHKeySchema(t *testing.T) { data := []byte(`{ "id": 2323, "name": "My key", @@ -1008,99 +608,77 @@ func TestSSHKeyFromSchema(t *testing.T) { }`) var s schema.SSHKey - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - sshKey := SSHKeyFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if sshKey.ID != 2323 { - t.Errorf("unexpected ID: %v", sshKey.ID) - } - if sshKey.Name != "My key" { - t.Errorf("unexpected name: %v", sshKey.Name) - } - if sshKey.Fingerprint != "b7:2f:30:a0:2f:6c:58:6c:21:04:58:61:ba:06:3b:2c" { - t.Errorf("unexpected fingerprint: %v", sshKey.Fingerprint) - } - if sshKey.PublicKey != "ssh-rsa AAAjjk76kgf...Xt" { - t.Errorf("unexpected public key: %v", sshKey.PublicKey) - } - if sshKey.Labels["key"] != "value" || sshKey.Labels["key2"] != "value2" { - t.Errorf("unexpected labels: %v", sshKey.Labels) - } - if !sshKey.Created.Equal(time.Date(2017, 8, 16, 17, 29, 14, 0, time.UTC)) { - t.Errorf("unexpected created date: %v", sshKey.Created) - } -} + assert.Equal(t, s, SchemaFromSSHKey(SSHKeyFromSchema(s))) -func TestErrorFromSchema(t *testing.T) { - t.Run("service_error", func(t *testing.T) { - data := []byte(`{ - "code": "service_error", - "message": "An error occurred", - "details": {} - }`) + key := SSHKeyFromSchema(s) + assert.Equal(t, key, SSHKeyFromSchema(SchemaFromSSHKey(key))) +} - var s schema.Error - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) +func TestVolumeSchema(t *testing.T) { + data := []byte(`{ + "id": 4711, + "created": "2016-01-30T23:50:11+00:00", + "name": "db-storage", + "status": "creating", + "server": 2, + "location": { + "id": 1, + "name": "fsn1", + "description": "Falkenstein DC Park 1", + "country": "DE", + "city": "Falkenstein", + "latitude": 50.47612, + "longitude": 12.370071 + }, + "size": 42, + "linux_device":"/dev/disk/by-id/scsi-0HC_volume_1", + "protection": { + "delete": true + }, + "labels": { + "key": "value", + "key2": "value2" } - err := ErrorFromSchema(s) + }`) - if err.Code != "service_error" { - t.Errorf("unexpected code: %v", err.Code) - } - if err.Message != "An error occurred" { - t.Errorf("unexpected message: %v", err.Message) - } - }) + var s schema.Volume + assert.NoError(t, json.Unmarshal(data, &s)) - t.Run("invalid_input", func(t *testing.T) { - data := []byte(`{ + assert.Equal(t, s, SchemaFromVolume(VolumeFromSchema(s))) + + v := VolumeFromSchema(s) + assert.Equal(t, v, VolumeFromSchema(SchemaFromVolume(v))) +} + +func TestErrorSchema(t *testing.T) { + testCases := map[string]string{ + "service_error": `{ + "code": "service_error", + "message": "An error occurred" + }`, + "invalid_input": `{ "code": "invalid_input", "message": "invalid input", - "details": { - "fields": [ - { - "name": "broken_field", - "messages": ["is required"] - } - ] - } - }`) + "details": {"fields":[{"name":"broken_field","messages":["is required"]}]} + }`, + } - var s schema.Error - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - err := ErrorFromSchema(s) + for name, data := range testCases { + t.Run(name, func(t *testing.T) { + var s schema.Error + assert.NoError(t, json.Unmarshal([]byte(data), &s)) - if err.Code != "invalid_input" { - t.Errorf("unexpected Code: %v", err.Code) - } - if err.Message != "invalid input" { - t.Errorf("unexpected Message: %v", err.Message) - } - if d, ok := err.Details.(ErrorDetailsInvalidInput); !ok { - t.Fatalf("unexpected Details type (should be ErrorDetailsInvalidInput): %v", err.Details) - } else { - if len(d.Fields) != 1 { - t.Fatalf("unexpected Details.Fields length (should be 1): %v", d.Fields) - } - if d.Fields[0].Name != "broken_field" { - t.Errorf("unexpected Details.Fields[0].Name: %v", d.Fields[0].Name) - } - if len(d.Fields[0].Messages) != 1 { - t.Fatalf("unexpected Details.Fields[0].Messages length (should be 1): %v", d.Fields[0].Messages) - } - if d.Fields[0].Messages[0] != "is required" { - t.Errorf("unexpected Details.Fields[0].Messages[0]: %v", d.Fields[0].Messages[0]) - } - } - }) + assert.Equal(t, s, SchemaFromError(ErrorFromSchema(s))) + + e := ErrorFromSchema(s) + assert.Equal(t, e, ErrorFromSchema(SchemaFromError(e))) + }) + } } -func TestPaginationFromSchema(t *testing.T) { +func TestPaginationSchema(t *testing.T) { data := []byte(`{ "page": 2, "per_page": 25, @@ -1111,32 +689,15 @@ func TestPaginationFromSchema(t *testing.T) { }`) var s schema.MetaPagination - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - p := PaginationFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if p.Page != 2 { - t.Errorf("unexpected page: %v", p.Page) - } - if p.PerPage != 25 { - t.Errorf("unexpected per page: %v", p.PerPage) - } - if p.PreviousPage != 1 { - t.Errorf("unexpected previous page: %v", p.PreviousPage) - } - if p.NextPage != 3 { - t.Errorf("unexpected next page: %d", p.NextPage) - } - if p.LastPage != 13 { - t.Errorf("unexpected last page: %d", p.LastPage) - } - if p.TotalEntries != 322 { - t.Errorf("unexpected total entries: %d", p.TotalEntries) - } + assert.Equal(t, s, SchemaFromPagination(PaginationFromSchema(s))) + + p := PaginationFromSchema(s) + assert.Equal(t, p, PaginationFromSchema(SchemaFromPagination(p))) } -func TestImageFromSchema(t *testing.T) { +func TestImageSchema(t *testing.T) { data := []byte(`{ "id": 4711, "type": "system", @@ -1167,134 +728,15 @@ func TestImageFromSchema(t *testing.T) { }`) var s schema.Image - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - image := ImageFromSchema(s) + assert.NoError(t, json.Unmarshal(data, &s)) - if image.ID != 4711 { - t.Errorf("unexpected ID: %v", image.ID) - } - if image.Type != ImageTypeSystem { - t.Errorf("unexpected Type: %v", image.Type) - } - if image.Status != ImageStatusAvailable { - t.Errorf("unexpected Status: %v", image.Status) - } - if image.Name != "ubuntu16.04-standard-x64" { - t.Errorf("unexpected Name: %v", image.Name) - } - if image.Description != "Ubuntu 16.04 Standard 64 bit" { - t.Errorf("unexpected Description: %v", image.Description) - } - if image.ImageSize != 2.3 { - t.Errorf("unexpected ImageSize: %v", image.ImageSize) - } - if image.DiskSize != 10 { - t.Errorf("unexpected DiskSize: %v", image.DiskSize) - } - if !image.Created.Equal(time.Date(2016, 1, 30, 23, 55, 1, 0, time.UTC)) { - t.Errorf("unexpected Created: %v", image.Created) - } - if !image.Deleted.Equal(time.Date(2016, 1, 30, 23, 55, 1, 0, time.UTC)) { - t.Errorf("unexpected Deleted: %v", image.Deleted) - } - if image.CreatedFrom == nil || image.CreatedFrom.ID != 1 || image.CreatedFrom.Name != "my-server1" { - t.Errorf("unexpected CreatedFrom: %v", image.CreatedFrom) - } - if image.BoundTo == nil || image.BoundTo.ID != 1 { - t.Errorf("unexpected BoundTo: %v", image.BoundTo) - } - if image.OSVersion != "16.04" { - t.Errorf("unexpected OSVersion: %v", image.OSVersion) - } - if image.OSFlavor != "ubuntu" { - t.Errorf("unexpected OSFlavor: %v", image.OSFlavor) - } - if image.Architecture != ArchitectureARM { - t.Errorf("unexpected architecture: %v", image.Architecture) - } - if image.RapidDeploy { - t.Errorf("unexpected RapidDeploy: %v", image.RapidDeploy) - } - if !image.Protection.Delete { - t.Errorf("unexpected Protection.Delete: %v", image.Protection.Delete) - } - if image.Deprecated.IsZero() { - t.Errorf("unexpected value for Deprecated: %v", image.Deprecated) - } - if image.Labels["key"] != "value" || image.Labels["key2"] != "value2" { - t.Errorf("unexpected Labels: %v", image.Labels) - } -} + assert.Equal(t, s, SchemaFromImage(ImageFromSchema(s))) -func TestVolumeFromSchema(t *testing.T) { - data := []byte(`{ - "id": 4711, - "created": "2016-01-30T23:50:11+00:00", - "name": "db-storage", - "status": "creating", - "server": 2, - "location": { - "id": 1, - "name": "fsn1", - "description": "Falkenstein DC Park 1", - "country": "DE", - "city": "Falkenstein", - "latitude": 50.47612, - "longitude": 12.370071 - }, - "size": 42, - "linux_device":"/dev/disk/by-id/scsi-0HC_volume_1", - "protection": { - "delete": true - }, - "labels": { - "key": "value", - "key2": "value2" - } - }`) - var s schema.Volume - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - volume := VolumeFromSchema(s) - if volume.ID != 4711 { - t.Errorf("unexpected ID: %v", volume.ID) - } - if volume.Name != "db-storage" { - t.Errorf("unexpected name: %v", volume.Name) - } - if volume.Status != VolumeStatusCreating { - t.Errorf("unexpected status: %v", volume.Status) - } - if !volume.Created.Equal(time.Date(2016, 1, 30, 23, 50, 11, 0, time.UTC)) { - t.Errorf("unexpected created date: %s", volume.Created) - } - if volume.Server == nil { - t.Error("no server") - } - if volume.Server != nil && volume.Server.ID != 2 { - t.Errorf("unexpected server ID: %v", volume.Server.ID) - } - if volume.Location == nil || volume.Location.ID != 1 { - t.Errorf("unexpected location: %v", volume.Location) - } - if volume.Size != 42 { - t.Errorf("unexpected size: %v", volume.Size) - } - if !volume.Protection.Delete { - t.Errorf("unexpected value for delete protection: %v", volume.Protection.Delete) - } - if len(volume.Labels) != 2 { - t.Errorf("unexpected number of labels: %d", len(volume.Labels)) - } - if volume.Labels["key"] != "value" || volume.Labels["key2"] != "value2" { - t.Errorf("unexpected labels: %v", volume.Labels) - } + img := ImageFromSchema(s) + assert.Equal(t, img, ImageFromSchema(SchemaFromImage(img))) } -func TestNetworkFromSchema(t *testing.T) { +func TestNetworkSchema(t *testing.T) { data := []byte(`{ "id": 4711, "name": "mynet", @@ -1324,119 +766,60 @@ func TestNetworkFromSchema(t *testing.T) { }`) var s schema.Network - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - network := NetworkFromSchema(s) - if network.ID != 4711 { - t.Errorf("unexpected ID: %v", network.ID) - } - if network.Name != "mynet" { - t.Errorf("unexpected Name: %v", network.Name) - } - if !network.Created.Equal(time.Date(2017, 8, 16, 17, 29, 14, 0, time.UTC)) { - t.Errorf("unexpected created date: %v", network.Created) - } - if network.IPRange.String() != "10.0.0.0/16" { - t.Errorf("unexpected IPRange: %v", network.IPRange) - } - if len(network.Subnets) != 1 { - t.Errorf("unexpected length of Subnets: %v", len(network.Subnets)) - } - if len(network.Routes) != 1 { - t.Errorf("unexpected length of Routes: %v", len(network.Routes)) - } - if len(network.Servers) != 1 { - t.Errorf("unexpected length of Servers: %v", len(network.Servers)) - } - if network.Servers[0].ID != 4711 { - t.Errorf("unexpected Server ID: %v", network.Servers[0].ID) - } - if network.Protection.Delete { - t.Errorf("unexpected value for delete protection: %v", network.Protection.Delete) - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromNetwork(NetworkFromSchema(s))) + + n := NetworkFromSchema(s) + assert.Equal(t, n, NetworkFromSchema(SchemaFromNetwork(n))) } -func TestNetworkSubnetFromSchema(t *testing.T) { - t.Run("type server", func(t *testing.T) { - data := []byte(`{ +func TestNetworkSubnetSchema(t *testing.T) { + testCases := map[string]string{ + "server_subnet": `{ "type": "server", "ip_range": "10.0.1.0/24", "network_zone": "eu-central", "gateway": "10.0.0.1" - }`) - var s schema.NetworkSubnet - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - networkSubnet := NetworkSubnetFromSchema(s) - if networkSubnet.NetworkZone != "eu-central" { - t.Errorf("unexpected NetworkZone: %v", networkSubnet.NetworkZone) - } - if networkSubnet.Type != "server" { - t.Errorf("unexpected Type: %v", networkSubnet.Type) - } - if networkSubnet.IPRange.String() != "10.0.1.0/24" { - t.Errorf("unexpected IPRange: %v", networkSubnet.IPRange) - } - if networkSubnet.Gateway.String() != "10.0.0.1" { - t.Errorf("unexpected Gateway: %v", networkSubnet.Gateway) - } - if networkSubnet.VSwitchID != 0 { - t.Errorf("unexpected VSwitchID: %v", networkSubnet.VSwitchID) - } - }) - - t.Run("type vswitch", func(t *testing.T) { - data := []byte(`{ + }`, + "vswitch_subnet": `{ "type": "vswitch", "ip_range": "10.0.1.0/24", "network_zone": "eu-central", "gateway": "10.0.0.1", "vswitch_id": 123 - }`) - var s schema.NetworkSubnet - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - networkSubnet := NetworkSubnetFromSchema(s) - if networkSubnet.NetworkZone != "eu-central" { - t.Errorf("unexpected NetworkZone: %v", networkSubnet.NetworkZone) - } - if networkSubnet.Type != "vswitch" { - t.Errorf("unexpected Type: %v", networkSubnet.Type) - } - if networkSubnet.IPRange.String() != "10.0.1.0/24" { - t.Errorf("unexpected IPRange: %v", networkSubnet.IPRange) - } - if networkSubnet.Gateway.String() != "10.0.0.1" { - t.Errorf("unexpected Gateway: %v", networkSubnet.Gateway) - } - if networkSubnet.VSwitchID != 123 { - t.Errorf("unexpected VSwitchID: %v", networkSubnet.VSwitchID) - } - }) + }`, + } + + for name, data := range testCases { + t.Run(name, func(t *testing.T) { + var s schema.NetworkSubnet + assert.NoError(t, json.Unmarshal([]byte(data), &s)) + + assert.Equal(t, s, SchemaFromNetworkSubnet(NetworkSubnetFromSchema(s))) + + n := NetworkSubnetFromSchema(s) + assert.Equal(t, n, NetworkSubnetFromSchema(SchemaFromNetworkSubnet(n))) + }) + } } -func TestNetworkRouteFromSchema(t *testing.T) { +func TestNetworkRouteSchema(t *testing.T) { data := []byte(`{ "destination": "10.100.1.0/24", "gateway": "10.0.1.1" }`) + var s schema.NetworkRoute - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - networkRoute := NetworkRouteFromSchema(s) - if networkRoute.Destination.String() != "10.100.1.0/24" { - t.Errorf("unexpected Destination: %v", networkRoute.Destination) - } - if networkRoute.Gateway.String() != "10.0.1.1" { - t.Errorf("unexpected Gateway: %v", networkRoute.Gateway) - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromNetworkRoute(NetworkRouteFromSchema(s))) + + r := NetworkRouteFromSchema(s) + assert.Equal(t, r, NetworkRouteFromSchema(SchemaFromNetworkRoute(r))) } -func TestLoadBalancerTypeFromSchema(t *testing.T) { +func TestLoadBalancerTypeSchema(t *testing.T) { data := []byte(`{ "id": 1, "name": "lx11", @@ -1460,54 +843,17 @@ func TestLoadBalancerTypeFromSchema(t *testing.T) { } ] }`) + var s schema.LoadBalancerType - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - loadBalancerType := LoadBalancerTypeFromSchema(s) - if loadBalancerType.ID != 1 { - t.Errorf("unexpected ID: %v", loadBalancerType.ID) - } - if loadBalancerType.Name != "lx11" { - t.Errorf("unexpected Name: %v", loadBalancerType.Name) - } - if loadBalancerType.Description != "LX11" { - t.Errorf("unexpected Description: %v", loadBalancerType.Description) - } - if loadBalancerType.MaxConnections != 20000 { - t.Errorf("unexpected MaxConnections: %v", loadBalancerType.MaxConnections) - } - if loadBalancerType.MaxServices != 3 { - t.Errorf("unexpected MaxServices: %v", loadBalancerType.MaxServices) - } - if loadBalancerType.MaxTargets != 25 { - t.Errorf("unexpected MaxTargets: %v", loadBalancerType.MaxTargets) - } - if loadBalancerType.MaxAssignedCertificates != 10 { - t.Errorf("unexpected MaxAssignedCertificates: %v", loadBalancerType.MaxAssignedCertificates) - } - if len(loadBalancerType.Pricings) != 1 { - t.Errorf("unexpected number of pricings: %d", len(loadBalancerType.Pricings)) - } else { - if loadBalancerType.Pricings[0].Location.Name != "fsn1" { - t.Errorf("unexpected location name: %v", loadBalancerType.Pricings[0].Location.Name) - } - if loadBalancerType.Pricings[0].Hourly.Net != "1" { - t.Errorf("unexpected hourly net price: %v", loadBalancerType.Pricings[0].Hourly.Net) - } - if loadBalancerType.Pricings[0].Hourly.Gross != "1.19" { - t.Errorf("unexpected hourly gross price: %v", loadBalancerType.Pricings[0].Hourly.Gross) - } - if loadBalancerType.Pricings[0].Monthly.Net != "1" { - t.Errorf("unexpected monthly net price: %v", loadBalancerType.Pricings[0].Monthly.Net) - } - if loadBalancerType.Pricings[0].Monthly.Gross != "1.19" { - t.Errorf("unexpected monthly gross price: %v", loadBalancerType.Pricings[0].Monthly.Gross) - } - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromLoadBalancerType(LoadBalancerTypeFromSchema(s))) + + lbt := LoadBalancerTypeFromSchema(s) + assert.Equal(t, lbt, LoadBalancerTypeFromSchema(SchemaFromLoadBalancerType(lbt))) } -func TestLoadBalancerFromSchema(t *testing.T) { +func TestLoadBalancerSchema(t *testing.T) { data := []byte(`{ "id": 4711, "name": "Web Frontend", @@ -1633,72 +979,17 @@ func TestLoadBalancerFromSchema(t *testing.T) { "type": "round_robin" } }`) + var s schema.LoadBalancer - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - loadBalancer := LoadBalancerFromSchema(s) - if loadBalancer.ID != 4711 { - t.Errorf("unexpected ID: %v", loadBalancer.ID) - } - if loadBalancer.Name != "Web Frontend" { - t.Errorf("unexpected Name: %v", loadBalancer.Name) - } - if loadBalancer.PublicNet.IPv4.IP.String() != "131.232.99.1" { - t.Errorf("unexpected IPv4: %v", loadBalancer.PublicNet.IPv4.IP) - } - if loadBalancer.PublicNet.IPv4.DNSPtr != "example.org" { - t.Errorf("unexpected IPv4.DNSPtr: %v", loadBalancer.PublicNet.IPv4.DNSPtr) - } - if loadBalancer.PublicNet.IPv6.IP.String() != "2001:db8::1" { - t.Errorf("unexpected IPv6: %v", loadBalancer.PublicNet.IPv6) - } - if loadBalancer.PublicNet.IPv6.DNSPtr != "example.com" { - t.Errorf("unexpected IPv6.DNSPtr: %v", loadBalancer.PublicNet.IPv6.DNSPtr) - } - if len(loadBalancer.PrivateNet) != 1 { - t.Errorf("unexpected length of PrivateNet: %v", len(loadBalancer.PrivateNet)) - } else { - if loadBalancer.PrivateNet[0].Network.ID != 4711 { - t.Errorf("unexpected Network ID: %v", loadBalancer.PrivateNet[0].Network.ID) - } - if loadBalancer.PrivateNet[0].IP.String() != "10.0.255.1" { - t.Errorf("unexpected Network IP: %v", loadBalancer.PrivateNet[0].IP) - } - } - if loadBalancer.Location == nil || loadBalancer.Location.ID != 1 { - t.Errorf("unexpected Location: %v", loadBalancer.Location) - } - if loadBalancer.LoadBalancerType == nil || loadBalancer.LoadBalancerType.ID != 1 { - t.Errorf("unexpected LoadBalancerType: %v", loadBalancer.LoadBalancerType) - } - if loadBalancer.Protection.Delete { - t.Errorf("unexpected value for delete protection: %v", loadBalancer.Protection.Delete) - } - if !loadBalancer.Created.Equal(time.Date(2016, 01, 30, 23, 50, 00, 0, time.UTC)) { - t.Errorf("unexpected created date: %v", loadBalancer.Created) - } - if len(loadBalancer.Services) != 1 { - t.Errorf("unexpected length of Services: %v", len(loadBalancer.Services)) - } - if len(loadBalancer.Targets) != 2 { - t.Errorf("unexpected length of Targets: %v", len(loadBalancer.Targets)) - } - if loadBalancer.Algorithm.Type != "round_robin" { - t.Errorf("unexpected Algorithm.Type: %v", loadBalancer.Algorithm.Type) - } - if loadBalancer.IncludedTraffic != 654321 { - t.Errorf("unexpected included traffic: %v", loadBalancer.IncludedTraffic) - } - if loadBalancer.OutgoingTraffic != 123456 { - t.Errorf("unexpected outgoing traffic: %v", loadBalancer.OutgoingTraffic) - } - if loadBalancer.IngoingTraffic != 7891011 { - t.Errorf("unexpected ingoing traffic: %v", loadBalancer.IngoingTraffic) - } + assert.NoError(t, json.Unmarshal(data, &s)) + + assert.Equal(t, s, SchemaFromLoadBalancer(LoadBalancerFromSchema(s))) + + lb := LoadBalancerFromSchema(s) + assert.Equal(t, lb, LoadBalancerFromSchema(SchemaFromLoadBalancer(lb))) } -func TestLoadBalancerServiceFromSchema(t *testing.T) { +func TestLoadBalancerServiceSchema(t *testing.T) { data := []byte(`{ "protocol": "http", "listen_port": 443, @@ -1728,124 +1019,33 @@ func TestLoadBalancerServiceFromSchema(t *testing.T) { } } }`) + var s schema.LoadBalancerService - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - loadBalancerService := LoadBalancerServiceFromSchema(s) - if loadBalancerService.Protocol != "http" { - t.Errorf("unexpected Protocol: %v", loadBalancerService.Protocol) - } - if loadBalancerService.ListenPort != 443 { - t.Errorf("unexpected ListenPort: %v", loadBalancerService.ListenPort) - } - if loadBalancerService.DestinationPort != 80 { - t.Errorf("unexpected DestinationPort: %v", loadBalancerService.DestinationPort) - } - if loadBalancerService.Proxyprotocol { - t.Errorf("unexpected ProxyProtocol: %v", loadBalancerService.Proxyprotocol) - } - if loadBalancerService.HTTP.CookieName != "HCLBSTICKY" { - t.Errorf("unexpected HTTP.CookieName: %v", loadBalancerService.HTTP.CookieName) - } - if loadBalancerService.HTTP.CookieLifetime.Seconds() != 300 { - t.Errorf("unexpected HTTP.CookieLifetime: %v", loadBalancerService.HTTP.CookieLifetime.Seconds()) - } - if loadBalancerService.HTTP.Certificates[0].ID != 897 { - t.Errorf("unexpected Certificates[0].ID : %v", loadBalancerService.HTTP.Certificates[0].ID) - } - if !loadBalancerService.HTTP.RedirectHTTP { - t.Errorf("unexpected HTTP.RedirectHTTP: %v", loadBalancerService.HTTP.RedirectHTTP) - } + assert.NoError(t, json.Unmarshal(data, &s)) - if !loadBalancerService.HTTP.StickySessions { - t.Errorf("unexpected HTTP.StickySessions: %v", loadBalancerService.HTTP.StickySessions) - } - if loadBalancerService.HealthCheck.Protocol != "http" { - t.Errorf("unexpected HealthCheck.Protocol: %v", loadBalancerService.HealthCheck.Protocol) - } - if loadBalancerService.HealthCheck.Port != 4711 { - t.Errorf("unexpected HealthCheck.Port: %v", loadBalancerService.HealthCheck.Port) - } - if loadBalancerService.HealthCheck.Interval.Seconds() != 15 { - t.Errorf("unexpected HealthCheck.Interval: %v", loadBalancerService.HealthCheck.Interval) - } - if loadBalancerService.HealthCheck.Timeout.Seconds() != 10 { - t.Errorf("unexpected HealthCheck.Timeout: %v", loadBalancerService.HealthCheck.Timeout) - } - if loadBalancerService.HealthCheck.Retries != 3 { - t.Errorf("unexpected HealthCheck.Retries: %v", loadBalancerService.HealthCheck.Retries) - } - if loadBalancerService.HealthCheck.HTTP.Domain != "example.com" { - t.Errorf("unexpected HealthCheck.HTTP.Domain: %v", loadBalancerService.HealthCheck.HTTP.Domain) - } - if loadBalancerService.HealthCheck.HTTP.Path != "/" { - t.Errorf("unexpected HealthCheck.HTTP.Path: %v", loadBalancerService.HealthCheck.HTTP.Path) - } - if loadBalancerService.HealthCheck.HTTP.Response != "" { - t.Errorf("unexpected HealthCheck.HTTP.Response: %v", loadBalancerService.HealthCheck.HTTP.Response) - } - if loadBalancerService.HealthCheck.HTTP.TLS { - t.Errorf("unexpected HealthCheck.HTTP.TLS: %v", loadBalancerService.HealthCheck.HTTP.TLS) - } - if len(loadBalancerService.HealthCheck.HTTP.StatusCodes) != 2 { - t.Errorf("unexpected len(HealthCheck.HTTP.StatusCodes): %v", len(loadBalancerService.HealthCheck.HTTP.StatusCodes)) - } else { - if loadBalancerService.HealthCheck.HTTP.StatusCodes[0] != "200" { - t.Errorf("unexpected HealthCheck.HTTP.StatusCodes[0]: %v", loadBalancerService.HealthCheck.HTTP.StatusCodes[0]) - } - if loadBalancerService.HealthCheck.HTTP.StatusCodes[1] != "201" { - t.Errorf("unexpected HealthCheck.HTTP.StatusCodes[1]: %v", loadBalancerService.HealthCheck.HTTP.StatusCodes[1]) - } - } + assert.Equal(t, s, SchemaFromLoadBalancerService(LoadBalancerServiceFromSchema(s))) + + lbs := LoadBalancerServiceFromSchema(s) + assert.Equal(t, lbs, LoadBalancerServiceFromSchema(SchemaFromLoadBalancerService(lbs))) } -func TestLoadBalancerTargetFromSchema(t *testing.T) { - t.Run("server target", func(t *testing.T) { - data := []byte(`{ - "type": "server", - "server": { - "id": 80 - }, - "label_selector": null, - "health_status": [ - { - "listen_port": 443, - "status": "healthy" - } - ], - "use_private_ip": false - }`) - var s schema.LoadBalancerTarget - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - loadBalancerTarget := LoadBalancerTargetFromSchema(s) - if loadBalancerTarget.Type != "server" { - t.Errorf("unexpected Type: %v", loadBalancerTarget.Type) - } - if loadBalancerTarget.Server == nil || loadBalancerTarget.Server.Server.ID != 80 { - t.Errorf("unexpected Server: %v", loadBalancerTarget.Server) - } - if loadBalancerTarget.LabelSelector != nil { - t.Errorf("unexpected LabelSelector.Selector: %v", loadBalancerTarget.LabelSelector) - } - if loadBalancerTarget.UsePrivateIP { - t.Errorf("unexpected UsePrivateIP: %v", loadBalancerTarget.UsePrivateIP) - } - if len(loadBalancerTarget.HealthStatus) != 1 { - t.Errorf("unexpected Health Status length: %v", len(loadBalancerTarget.HealthStatus)) - } else { - if loadBalancerTarget.HealthStatus[0].ListenPort != 443 { - t.Errorf("unexpected HealthStatus[0].ListenPort: %v", loadBalancerTarget.HealthStatus[0].ListenPort) - } - if loadBalancerTarget.HealthStatus[0].Status != LoadBalancerTargetHealthStatusStatusHealthy { - t.Errorf("unexpected HealthStatus[0].Status: %v", loadBalancerTarget.HealthStatus[0].Status) - } - } - }) - t.Run("label_selector target", func(t *testing.T) { - data := []byte(`{ +func TestLoadBalancerTargetSchema(t *testing.T) { + testCases := map[string]string{ + "server target": `{ + "type": "server", + "server": { + "id": 80 + }, + "label_selector": null, + "health_status": [ + { + "listen_port": 443, + "status": "healthy" + } + ], + "use_private_ip": false + }`, + "label_selector target": `{ "type": "label_selector", "label_selector": { "selector": "lbt" @@ -1861,70 +1061,36 @@ func TestLoadBalancerTargetFromSchema(t *testing.T) { "listen_port": 443, "status": "healthy" } - ] - } - ] - }`) - var s schema.LoadBalancerTarget - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - loadBalancerTarget := LoadBalancerTargetFromSchema(s) - if loadBalancerTarget.Type != "label_selector" { - t.Errorf("unexpected Type: %v", loadBalancerTarget.Type) - } - if loadBalancerTarget.LabelSelector == nil || loadBalancerTarget.LabelSelector.Selector != "lbt" { - t.Errorf("unexpected LabelSelector: %v", loadBalancerTarget.LabelSelector) - } - if loadBalancerTarget.Server != nil { - t.Errorf("unexpected LabelSelector.Server: %v", loadBalancerTarget.Server) - } - if len(loadBalancerTarget.Targets) != 1 { - t.Errorf("unexpected Targets length: %v", len(loadBalancerTarget.Targets)) - } else { - if loadBalancerTarget.Targets[0].Server == nil || loadBalancerTarget.Targets[0].Server.Server.ID != 80 { - t.Errorf("unexpected loadBalancerTarget.Targets[0].Server.Server.ID: %v", loadBalancerTarget.Targets[0].Server.Server.ID) - } - if len(loadBalancerTarget.Targets[0].HealthStatus) != 1 { - t.Errorf("unexpected Targets length: %v", len(loadBalancerTarget.Targets[0].HealthStatus)) - } else { - if loadBalancerTarget.Targets[0].HealthStatus[0].ListenPort != 443 { - t.Errorf("unexpected HealthStatus[0].ListenPort: %v", loadBalancerTarget.Targets[0].HealthStatus[0].ListenPort) - } - if loadBalancerTarget.Targets[0].HealthStatus[0].Status != LoadBalancerTargetHealthStatusStatusHealthy { - t.Errorf("unexpected HealthStatus[0].Status: %v", loadBalancerTarget.Targets[0].HealthStatus[0].Status) + ] } - } - } - }) - - t.Run("ip target", func(t *testing.T) { - var s schema.LoadBalancerTarget - - data := []byte(`{ + ] + }`, + "ip target": `{ "type": "ip", "ip": { "ip": "1.2.3.4" } - }`) - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - lbTgt := LoadBalancerTargetFromSchema(s) - if lbTgt.Type != LoadBalancerTargetTypeIP { - t.Errorf("unexpected Type: %s", lbTgt.Type) - } - if lbTgt.IP.IP != "1.2.3.4" { - t.Errorf("unexpected IP: %s", lbTgt.IP.IP) - } - }) + }`, + } + + for name, data := range testCases { + t.Run(name, func(t *testing.T) { + var s schema.LoadBalancerTarget + assert.NoError(t, json.Unmarshal([]byte(data), &s)) + + assert.Equal(t, s, SchemaFromLoadBalancerTarget(LoadBalancerTargetFromSchema(s))) + + lbt := LoadBalancerTargetFromSchema(s) + assert.Equal(t, lbt, LoadBalancerTargetFromSchema(SchemaFromLoadBalancerTarget(lbt))) + }) + } } -func TestCertificateFromSchema(t *testing.T) { - tests := []struct { +func TestCertificateSchema(t *testing.T) { + testCases := []struct { name string data string - expected Certificate + expected *Certificate }{ { name: "uploaded certificate", @@ -1947,9 +1113,10 @@ func TestCertificateFromSchema(t *testing.T) { {"id": 42, "type": "loadbalancer"} ] }`, - expected: Certificate{ + expected: &Certificate{ ID: 897, Name: "my website cert", + Labels: map[string]string{}, Type: "uploaded", Certificate: "-----BEGIN CERTIFICATE-----\n...", Created: mustParseTime(t, "2016-01-30T23:50:00+00:00"), @@ -1991,9 +1158,10 @@ func TestCertificateFromSchema(t *testing.T) { {"id": 42, "type": "loadbalancer"} ] }`, - expected: Certificate{ + expected: &Certificate{ ID: 898, Name: "managed certificate", + Labels: map[string]string{}, Type: "managed", Certificate: "-----BEGIN CERTIFICATE-----\n...", Created: mustParseTime(t, "2016-01-30T23:50:00+00:00"), @@ -2015,20 +1183,24 @@ func TestCertificateFromSchema(t *testing.T) { }, }, } - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { + for _, testCase := range testCases { + testCase := testCase + t.Run(testCase.name, func(t *testing.T) { var s schema.Certificate + assert.NoError(t, json.Unmarshal([]byte(testCase.data), &s)) + + assert.Equal(t, s, SchemaFromCertificate(CertificateFromSchema(s))) + + ct := CertificateFromSchema(s) + assert.Equal(t, ct, CertificateFromSchema(SchemaFromCertificate(ct))) - err := json.Unmarshal([]byte(tt.data), &s) - assert.NoError(t, err) - actual := CertificateFromSchema(s) - assert.Equal(t, &tt.expected, actual) + assert.Equal(t, testCase.expected, CertificateFromSchema(s)) + assert.Equal(t, s, SchemaFromCertificate(testCase.expected)) }) } } -func TestPricingFromSchema(t *testing.T) { +func TestPricingSchema(t *testing.T) { data := []byte(`{ "currency": "EUR", "vat_rate": "19.00", @@ -2132,217 +1304,113 @@ func TestPricingFromSchema(t *testing.T) { }`) var s schema.Pricing - if err := json.Unmarshal(data, &s); err != nil { - t.Fatal(err) - } - pricing := PricingFromSchema(s) - - if pricing.Image.PerGBMonth.Currency != "EUR" { - t.Errorf("unexpected Image.PerGBMonth.Currency: %v", pricing.Image.PerGBMonth.Currency) - } - if pricing.Image.PerGBMonth.VATRate != "19.00" { - t.Errorf("unexpected Image.PerGBMonth.VATRate: %v", pricing.Image.PerGBMonth.VATRate) - } - if pricing.Image.PerGBMonth.Net != "1" { - t.Errorf("unexpected Image.PerGBMonth.Net: %v", pricing.Image.PerGBMonth.Net) - } - if pricing.Image.PerGBMonth.Gross != "1.19" { - t.Errorf("unexpected Image.PerGBMonth.Gross: %v", pricing.Image.PerGBMonth.Gross) - } - - if pricing.FloatingIP.Monthly.Currency != "EUR" { - t.Errorf("unexpected FloatingIP.Monthly.Currency: %v", pricing.FloatingIP.Monthly.Currency) - } - if pricing.FloatingIP.Monthly.VATRate != "19.00" { - t.Errorf("unexpected FloatingIP.Monthly.VATRate: %v", pricing.FloatingIP.Monthly.VATRate) - } - if pricing.FloatingIP.Monthly.Net != "1" { - t.Errorf("unexpected FloatingIP.Monthly.Net: %v", pricing.FloatingIP.Monthly.Net) - } - if pricing.FloatingIP.Monthly.Gross != "1.19" { - t.Errorf("unexpected FloatingIP.Monthly.Gross: %v", pricing.FloatingIP.Monthly.Gross) - } - - if len(pricing.FloatingIPs) != 1 { - t.Errorf("unexpected number of Floating IPs: %d", len(pricing.FloatingIPs)) - } else { - p := pricing.FloatingIPs[0] + assert.NoError(t, json.Unmarshal(data, &s)) - if p.Type != FloatingIPTypeIPv4 { - t.Errorf("unexpected .Type: %s", p.Type) - } - if len(p.Pricings) != 1 { - t.Errorf("unexpected number of prices: %d", len(p.Pricings)) - } else { - if p.Pricings[0].Location.Name != "fsn1" { - t.Errorf("unexpected Location.Name: %v", p.Pricings[0].Location.Name) - } - if p.Pricings[0].Monthly.Currency != "EUR" { - t.Errorf("unexpected Monthly.Currency: %v", p.Pricings[0].Monthly.Currency) - } - if p.Pricings[0].Monthly.VATRate != "19.00" { - t.Errorf("unexpected Monthly.VATRate: %v", p.Pricings[0].Monthly.VATRate) - } - if p.Pricings[0].Monthly.Net != "1" { - t.Errorf("unexpected Monthly.Net: %v", p.Pricings[0].Monthly.Net) - } - if p.Pricings[0].Monthly.Gross != "1.19" { - t.Errorf("unexpected Monthly.Gross: %v", p.Pricings[0].Monthly.Gross) - } - } - } + assert.Equal(t, s, SchemaFromPricing(PricingFromSchema(s))) - if len(pricing.PrimaryIPs) != 1 { - t.Errorf("unexpected number of Primary IPs: %d", len(pricing.PrimaryIPs)) - } else { - ip := pricing.PrimaryIPs[0] + p := PricingFromSchema(s) + assert.Equal(t, p, PricingFromSchema(SchemaFromPricing(p))) +} - if ip.Type != "ipv4" { - t.Errorf("unexpected .Type: %s", ip.Type) - } - if len(ip.Pricings) != 1 { - t.Errorf("unexpected number of prices: %d", len(ip.Pricings)) - } else { - if ip.Pricings[0].Location != "fsn1" { - t.Errorf("unexpected Location: %v", ip.Pricings[0].Location) - } - if ip.Pricings[0].Monthly.Net != "1.0000000000" { - t.Errorf("unexpected Monthly.Net: %v", ip.Pricings[0].Monthly.Net) - } - if ip.Pricings[0].Monthly.Gross != "1.1900000000000000" { - t.Errorf("unexpected Monthly.Gross: %v", ip.Pricings[0].Monthly.Gross) - } - if ip.Pricings[0].Hourly.Net != "1.0000000000" { - t.Errorf("unexpected Hourly.Net: %v", ip.Pricings[0].Hourly.Net) +func TestFirewallSchema(t *testing.T) { + data := []byte(`{ + "id": 897, + "name": "my firewall", + "labels": { + "key": "value", + "key2": "value2" + }, + "created": "2016-01-30T23:50:00+00:00", + "rules": [ + { + "direction": "in", + "source_ips": [ + "28.239.13.1/32", + "28.239.14.0/24", + "ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128" + ], + "destination_ips": [ + "28.239.13.1/32", + "28.239.14.0/24", + "ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128" + ], + "protocol": "tcp", + "port": "80", + "description": "allow http in" } - if ip.Pricings[0].Hourly.Gross != "1.1900000000000000" { - t.Errorf("unexpected Hourly.Gross: %v", ip.Pricings[0].Hourly.Gross) + ], + "applied_to": [ + { + "server": { + "id": 42 + }, + "type": "server" + }, + { + "label_selector": { + "selector": "a=b" + }, + "type": "label_selector" } - } - } + ] + }`) - if pricing.Volume.PerGBMonthly.Currency != "EUR" { - t.Errorf("unexpected Traffic.PerTB.Currency: %v", pricing.Volume.PerGBMonthly.Currency) - } - if pricing.Volume.PerGBMonthly.VATRate != "19.00" { - t.Errorf("unexpected Traffic.PerTB.VATRate: %v", pricing.Volume.PerGBMonthly.VATRate) - } - if pricing.Volume.PerGBMonthly.Net != "1" { - t.Errorf("unexpected Traffic.PerTB.Net: %v", pricing.Volume.PerGBMonthly.Net) - } - if pricing.Volume.PerGBMonthly.Gross != "1.19" { - t.Errorf("unexpected Traffic.PerTB.Gross: %v", pricing.Volume.PerGBMonthly.Gross) - } + var s schema.Firewall + assert.NoError(t, json.Unmarshal(data, &s)) - if pricing.Traffic.PerTB.Currency != "EUR" { - t.Errorf("unexpected Traffic.PerTB.Currency: %v", pricing.Traffic.PerTB.Currency) - } - if pricing.Traffic.PerTB.VATRate != "19.00" { - t.Errorf("unexpected Traffic.PerTB.VATRate: %v", pricing.Traffic.PerTB.VATRate) - } - if pricing.Traffic.PerTB.Net != "1" { - t.Errorf("unexpected Traffic.PerTB.Net: %v", pricing.Traffic.PerTB.Net) - } - if pricing.Traffic.PerTB.Gross != "1.19" { - t.Errorf("unexpected Traffic.PerTB.Gross: %v", pricing.Traffic.PerTB.Gross) - } + assert.Equal(t, s, SchemaFromFirewall(FirewallFromSchema(s))) - if pricing.ServerBackup.Percentage != "20" { - t.Errorf("unexpected ServerBackup.Percentage: %v", pricing.ServerBackup.Percentage) - } + fw := FirewallFromSchema(s) + assert.Equal(t, fw, FirewallFromSchema(SchemaFromFirewall(fw))) +} - if len(pricing.ServerTypes) != 1 { - t.Errorf("unexpected number of server types: %d", len(pricing.ServerTypes)) - } else { - p := pricing.ServerTypes[0] +func TestPlacementGroupSchema(t *testing.T) { + data := []byte(`{ + "created": "2019-01-08T12:10:00+00:00", + "id": 897, + "labels": { + "key": "value" + }, + "name": "my Placement Group", + "servers": [ + 4711, + 4712 + ], + "type": "spread" + }`) - if p.ServerType.ID != 4 { - t.Errorf("unexpected ServerType.ID: %d", p.ServerType.ID) - } - if p.ServerType.Name != "CX11" { - t.Errorf("unexpected ServerType.Name: %v", p.ServerType.Name) - } + var s schema.PlacementGroup + assert.NoError(t, json.Unmarshal(data, &s)) - if len(p.Pricings) != 1 { - t.Errorf("unexpected number of prices: %d", len(p.Pricings)) - } else { - if p.Pricings[0].Location.Name != "fsn1" { - t.Errorf("unexpected Location.Name: %v", p.Pricings[0].Location.Name) - } + assert.Equal(t, s, SchemaFromPlacementGroup(PlacementGroupFromSchema(s))) - if p.Pricings[0].Hourly.Currency != "EUR" { - t.Errorf("unexpected Hourly.Currency: %v", p.Pricings[0].Hourly.Currency) - } - if p.Pricings[0].Hourly.VATRate != "19.00" { - t.Errorf("unexpected Hourly.VATRate: %v", p.Pricings[0].Hourly.VATRate) - } - if p.Pricings[0].Hourly.Net != "1" { - t.Errorf("unexpected Hourly.Net: %v", p.Pricings[0].Hourly.Net) - } - if p.Pricings[0].Hourly.Gross != "1.19" { - t.Errorf("unexpected Hourly.Gross: %v", p.Pricings[0].Hourly.Gross) - } + pg := PlacementGroupFromSchema(s) + assert.Equal(t, pg, PlacementGroupFromSchema(SchemaFromPlacementGroup(pg))) +} - if p.Pricings[0].Monthly.Currency != "EUR" { - t.Errorf("unexpected Monthly.Currency: %v", p.Pricings[0].Monthly.Currency) - } - if p.Pricings[0].Monthly.VATRate != "19.00" { - t.Errorf("unexpected Monthly.VATRate: %v", p.Pricings[0].Monthly.VATRate) - } - if p.Pricings[0].Monthly.Net != "1" { - t.Errorf("unexpected Monthly.Net: %v", p.Pricings[0].Monthly.Net) - } - if p.Pricings[0].Monthly.Gross != "1.19" { - t.Errorf("unexpected Monthly.Gross: %v", p.Pricings[0].Monthly.Gross) +func TestDeprecationSchema(t *testing.T) { + testCases := map[string]string{ + "deprecated_resource": `{ + "deprecation": { + "announced": "2023-06-01T00:00:00+00:00", + "unavailable_after": "2023-09-01T00:00:00+00:00" } - } + }`, + "non_deprecated_resource": `{ + "deprecation": null + }`, } - if len(pricing.LoadBalancerTypes) != 1 { - t.Errorf("unexpected number of Load Balancer types: %d", len(pricing.LoadBalancerTypes)) - } else { - p := pricing.LoadBalancerTypes[0] - - if p.LoadBalancerType.ID != 4 { - t.Errorf("unexpected LoadBalancerType.ID: %d", p.LoadBalancerType.ID) - } - if p.LoadBalancerType.Name != "LX11" { - t.Errorf("unexpected LoadBalancerType.Name: %v", p.LoadBalancerType.Name) - } - - if len(p.Pricings) != 1 { - t.Errorf("unexpected number of prices: %d", len(p.Pricings)) - } else { - if p.Pricings[0].Location.Name != "fsn1" { - t.Errorf("unexpected Location.Name: %v", p.Pricings[0].Location.Name) - } + for name, data := range testCases { + t.Run(name, func(t *testing.T) { + var s *schema.DeprecationInfo + assert.NoError(t, json.Unmarshal([]byte(data), &s)) - if p.Pricings[0].Hourly.Currency != "EUR" { - t.Errorf("unexpected Hourly.Currency: %v", p.Pricings[0].Hourly.Currency) - } - if p.Pricings[0].Hourly.VATRate != "19.00" { - t.Errorf("unexpected Hourly.VATRate: %v", p.Pricings[0].Hourly.VATRate) - } - if p.Pricings[0].Hourly.Net != "1" { - t.Errorf("unexpected Hourly.Net: %v", p.Pricings[0].Hourly.Net) - } - if p.Pricings[0].Hourly.Gross != "1.19" { - t.Errorf("unexpected Hourly.Gross: %v", p.Pricings[0].Hourly.Gross) - } + assert.Equal(t, s, SchemaFromDeprecation(DeprecationFromSchema(s))) - if p.Pricings[0].Monthly.Currency != "EUR" { - t.Errorf("unexpected Monthly.Currency: %v", p.Pricings[0].Monthly.Currency) - } - if p.Pricings[0].Monthly.VATRate != "19.00" { - t.Errorf("unexpected Monthly.VATRate: %v", p.Pricings[0].Monthly.VATRate) - } - if p.Pricings[0].Monthly.Net != "1" { - t.Errorf("unexpected Monthly.Net: %v", p.Pricings[0].Monthly.Net) - } - if p.Pricings[0].Monthly.Gross != "1.19" { - t.Errorf("unexpected Monthly.Gross: %v", p.Pricings[0].Monthly.Gross) - } - } + d := DeprecationFromSchema(s) + assert.Equal(t, d, DeprecationFromSchema(SchemaFromDeprecation(d))) + }) } } @@ -3085,183 +2153,3 @@ func TestLoadBalancerMetricsFromSchema(t *testing.T) { }) } } - -func TestFirewallFromSchema(t *testing.T) { - data := []byte(`{ - "id": 897, - "name": "my firewall", - "labels": { - "key": "value", - "key2": "value2" - }, - "created": "2016-01-30T23:50:00+00:00", - "rules": [ - { - "direction": "in", - "source_ips": [ - "28.239.13.1/32", - "28.239.14.0/24", - "ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128" - ], - "destination_ips": [ - "28.239.13.1/32", - "28.239.14.0/24", - "ff21:1eac:9a3b:ee58:5ca:990c:8bc9:c03b/128" - ], - "protocol": "tcp", - "port": "80", - "description": "allow http in" - } - ], - "applied_to": [ - { - "server": { - "id": 42 - }, - "type": "server" - }, - { - "label_selector": { - "selector": "a=b" - }, - "type": "label_selector" - } - ] - } -`) - var f schema.Firewall - if err := json.Unmarshal(data, &f); err != nil { - t.Fatal(err) - } - firewall := FirewallFromSchema(f) - - if firewall.ID != 897 { - t.Errorf("unexpected ID: %v", firewall.ID) - } - if firewall.Name != "my firewall" { - t.Errorf("unexpected Name: %v", firewall.Name) - } - if firewall.Labels["key"] != "value" || firewall.Labels["key2"] != "value2" { - t.Errorf("unexpected Labels: %v", firewall.Labels) - } - if !firewall.Created.Equal(time.Date(2016, 01, 30, 23, 50, 00, 0, time.UTC)) { - t.Errorf("unexpected Created date: %v", firewall.Created) - } - if len(firewall.Rules) != 1 { - t.Errorf("unexpected Rules count: %d", len(firewall.Rules)) - } - if firewall.Rules[0].Direction != FirewallRuleDirectionIn { - t.Errorf("unexpected Rule Direction: %s", firewall.Rules[0].Direction) - } - if len(firewall.Rules[0].SourceIPs) != 3 { - t.Errorf("unexpected Rule SourceIPs count: %d", len(firewall.Rules[0].SourceIPs)) - } - if len(firewall.Rules[0].DestinationIPs) != 3 { - t.Errorf("unexpected Rule DestinationIPs count: %d", len(firewall.Rules[0].DestinationIPs)) - } - if firewall.Rules[0].Protocol != FirewallRuleProtocolTCP { - t.Errorf("unexpected Rule Protocol: %s", firewall.Rules[0].Protocol) - } - if *firewall.Rules[0].Port != "80" { - t.Errorf("unexpected Rule Port: %s", *firewall.Rules[0].Port) - } - if *firewall.Rules[0].Description != "allow http in" { - t.Errorf("unexpected Rule Description: %s", *firewall.Rules[0].Description) - } - if len(firewall.AppliedTo) != 2 { - t.Errorf("unexpected UsedBy count: %d", len(firewall.AppliedTo)) - } - if firewall.AppliedTo[0].Type != FirewallResourceTypeServer { - t.Errorf("unexpected UsedBy Type: %s", firewall.AppliedTo[0].Type) - } - if firewall.AppliedTo[0].Server.ID != 42 { - t.Errorf("unexpected UsedBy Server ID: %d", firewall.AppliedTo[0].Server.ID) - } - if firewall.AppliedTo[1].Type != FirewallResourceTypeLabelSelector { - t.Errorf("unexpected UsedBy Type: %s", firewall.AppliedTo[0].Type) - } - if firewall.AppliedTo[1].LabelSelector.Selector != "a=b" { - t.Errorf("unexpected UsedBy Label Selector: %s", firewall.AppliedTo[1].LabelSelector.Selector) - } -} - -func TestPlacementGroupFromSchema(t *testing.T) { - data := []byte(`{ - "created": "2019-01-08T12:10:00+00:00", - "id": 897, - "labels": { - "key": "value" - }, - "name": "my Placement Group", - "servers": [ - 4711, - 4712 - ], - "type": "spread" - } -`) - - var g schema.PlacementGroup - if err := json.Unmarshal(data, &g); err != nil { - t.Fatal(err) - } - placementGroup := PlacementGroupFromSchema(g) - if placementGroup.ID != 897 { - t.Errorf("unexpected ID %d", placementGroup.ID) - } - if placementGroup.Name != "my Placement Group" { - t.Errorf("unexpected Name %s", placementGroup.Name) - } - if placementGroup.Labels["key"] != "value" { - t.Errorf("unexpected Labels: %v", placementGroup.Labels) - } - if !placementGroup.Created.Equal(time.Date(2019, 01, 8, 12, 10, 00, 0, time.UTC)) { - t.Errorf("unexpected Created date: %v", placementGroup.Created) - } - if len(placementGroup.Servers) != 2 { - t.Errorf("unexpected Servers %v", placementGroup.Servers) - } - if placementGroup.Type != PlacementGroupTypeSpread { - t.Errorf("unexpected Type %s", placementGroup.Type) - } -} - -func TestDeprecationFromSchema(t *testing.T) { - t.Run("Deprecated Resource", func(t *testing.T) { - data := []byte(`{ - "deprecation": { - "announced": "2023-06-01T00:00:00+00:00", - "unavailable_after": "2023-09-01T00:00:00+00:00" - } - }`) - - var d schema.DeprecatableResource - if err := json.Unmarshal(data, &d); err != nil { - t.Fatal(err) - } - deprecationInfo := DeprecationFromSchema(d.Deprecation) - if deprecationInfo == nil { - t.Fatal("unexpected nil DeprecationInfo") - } - if deprecationInfo.Announced != mustParseTime(t, "2023-06-01T00:00:00+00:00") { - t.Errorf("unexpected anounce time %s", deprecationInfo.Announced) - } - if deprecationInfo.UnavailableAfter != mustParseTime(t, "2023-09-01T00:00:00+00:00") { - t.Errorf("unexpected unavailable after time %s", deprecationInfo.UnavailableAfter) - } - }) - - t.Run("Not-deprecated Resource", func(t *testing.T) { - data := []byte(`{ - "deprecation": null - }`) - var d schema.DeprecatableResource - if err := json.Unmarshal(data, &d); err != nil { - t.Fatal(err) - } - deprecationInfo := DeprecationFromSchema(d.Deprecation) - if deprecationInfo != nil { - t.Fatal("unexpected non-nil DeprecationInfo", deprecationInfo) - } - }) -} diff --git a/hcloud/zz_schema.go b/hcloud/zz_schema.go new file mode 100755 index 000000000..5ba9b6c84 --- /dev/null +++ b/hcloud/zz_schema.go @@ -0,0 +1,2468 @@ +// Code generated by github.com/jmattheis/goverter, DO NOT EDIT. + +package hcloud + +import ( + schema "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" + "net" + "time" +) + +type converterImpl struct{} + +func (c *converterImpl) ActionFromSchema(source schema.Action) *Action { + var hcloudAction Action + hcloudAction.ID = source.ID + hcloudAction.Status = ActionStatus(source.Status) + hcloudAction.Command = source.Command + hcloudAction.Progress = source.Progress + hcloudAction.Started = source.Started + hcloudAction.Finished = c.pTimeTimeToTimeTime(source.Finished) + var pString *string + if source.Error != nil { + pString = &source.Error.Code + } + var xstring string + if pString != nil { + xstring = *pString + } + hcloudAction.ErrorCode = xstring + var pString2 *string + if source.Error != nil { + pString2 = &source.Error.Message + } + var xstring2 string + if pString2 != nil { + xstring2 = *pString2 + } + hcloudAction.ErrorMessage = xstring2 + var pHcloudActionResourceList []*ActionResource + if source.Resources != nil { + pHcloudActionResourceList = make([]*ActionResource, len(source.Resources)) + for i := 0; i < len(source.Resources); i++ { + pHcloudActionResourceList[i] = c.schemaActionResourceReferenceToPHcloudActionResource(source.Resources[i]) + } + } + hcloudAction.Resources = pHcloudActionResourceList + return &hcloudAction +} +func (c *converterImpl) ActionsFromSchema(source []schema.Action) []*Action { + var pHcloudActionList []*Action + if source != nil { + pHcloudActionList = make([]*Action, len(source)) + for i := 0; i < len(source); i++ { + pHcloudActionList[i] = c.ActionFromSchema(source[i]) + } + } + return pHcloudActionList +} +func (c *converterImpl) CertificateFromSchema(source schema.Certificate) *Certificate { + var hcloudCertificate Certificate + hcloudCertificate.ID = source.ID + hcloudCertificate.Name = source.Name + hcloudCertificate.Labels = source.Labels + hcloudCertificate.Type = CertificateType(source.Type) + hcloudCertificate.Certificate = source.Certificate + hcloudCertificate.Created = c.timeTimeToTimeTime(source.Created) + hcloudCertificate.NotValidBefore = c.timeTimeToTimeTime(source.NotValidBefore) + hcloudCertificate.NotValidAfter = c.timeTimeToTimeTime(source.NotValidAfter) + hcloudCertificate.DomainNames = source.DomainNames + hcloudCertificate.Fingerprint = source.Fingerprint + hcloudCertificate.Status = c.pSchemaCertificateStatusRefToPHcloudCertificateStatus(source.Status) + var hcloudCertificateUsedByRefList []CertificateUsedByRef + if source.UsedBy != nil { + hcloudCertificateUsedByRefList = make([]CertificateUsedByRef, len(source.UsedBy)) + for i := 0; i < len(source.UsedBy); i++ { + hcloudCertificateUsedByRefList[i] = c.schemaCertificateUsedByRefToHcloudCertificateUsedByRef(source.UsedBy[i]) + } + } + hcloudCertificate.UsedBy = hcloudCertificateUsedByRefList + return &hcloudCertificate +} +func (c *converterImpl) DatacenterFromSchema(source schema.Datacenter) *Datacenter { + var hcloudDatacenter Datacenter + hcloudDatacenter.ID = source.ID + hcloudDatacenter.Name = source.Name + hcloudDatacenter.Description = source.Description + hcloudDatacenter.Location = c.LocationFromSchema(source.Location) + hcloudDatacenter.ServerTypes = c.unnamedToHcloudDatacenterServerTypes(source.ServerTypes) + return &hcloudDatacenter +} +func (c *converterImpl) DeprecationFromSchema(source *schema.DeprecationInfo) *DeprecationInfo { + var pHcloudDeprecationInfo *DeprecationInfo + if source != nil { + var hcloudDeprecationInfo DeprecationInfo + hcloudDeprecationInfo.Announced = c.timeTimeToTimeTime((*source).Announced) + hcloudDeprecationInfo.UnavailableAfter = c.timeTimeToTimeTime((*source).UnavailableAfter) + pHcloudDeprecationInfo = &hcloudDeprecationInfo + } + return pHcloudDeprecationInfo +} +func (c *converterImpl) ErrorFromSchema(source schema.Error) Error { + var hcloudError Error + hcloudError.Code = ErrorCode(source.Code) + hcloudError.Message = source.Message + hcloudError.Details = errorDetailsFromSchema(source.Details) + return hcloudError +} +func (c *converterImpl) FirewallFromSchema(source schema.Firewall) *Firewall { + var hcloudFirewall Firewall + hcloudFirewall.ID = source.ID + hcloudFirewall.Name = source.Name + hcloudFirewall.Labels = source.Labels + hcloudFirewall.Created = c.timeTimeToTimeTime(source.Created) + var hcloudFirewallRuleList []FirewallRule + if source.Rules != nil { + hcloudFirewallRuleList = make([]FirewallRule, len(source.Rules)) + for i := 0; i < len(source.Rules); i++ { + hcloudFirewallRuleList[i] = c.schemaFirewallRuleToHcloudFirewallRule(source.Rules[i]) + } + } + hcloudFirewall.Rules = hcloudFirewallRuleList + var hcloudFirewallResourceList []FirewallResource + if source.AppliedTo != nil { + hcloudFirewallResourceList = make([]FirewallResource, len(source.AppliedTo)) + for j := 0; j < len(source.AppliedTo); j++ { + hcloudFirewallResourceList[j] = c.schemaFirewallResourceToHcloudFirewallResource(source.AppliedTo[j]) + } + } + hcloudFirewall.AppliedTo = hcloudFirewallResourceList + return &hcloudFirewall +} +func (c *converterImpl) FloatingIPFromSchema(source schema.FloatingIP) *FloatingIP { + var hcloudFloatingIP FloatingIP + hcloudFloatingIP.ID = source.ID + var xstring string + if source.Description != nil { + xstring = *source.Description + } + hcloudFloatingIP.Description = xstring + hcloudFloatingIP.Created = c.timeTimeToTimeTime(source.Created) + hcloudFloatingIP.IP = ipFromFloatingIPSchema(source) + hcloudFloatingIP.Network = networkFromFloatingIPSchema(source) + hcloudFloatingIP.Type = FloatingIPType(source.Type) + var pHcloudServer *Server + if source.Server != nil { + hcloudServer := serverFromInt64(*source.Server) + pHcloudServer = &hcloudServer + } + hcloudFloatingIP.Server = pHcloudServer + hcloudFloatingIP.DNSPtr = mapFromFloatingIPDNSPtrSchema(source.DNSPtr) + hcloudFloatingIP.HomeLocation = c.LocationFromSchema(source.HomeLocation) + hcloudFloatingIP.Blocked = source.Blocked + hcloudFloatingIP.Protection = c.schemaFloatingIPProtectionToHcloudFloatingIPProtection(source.Protection) + hcloudFloatingIP.Labels = source.Labels + hcloudFloatingIP.Name = source.Name + return &hcloudFloatingIP +} +func (c *converterImpl) ISOFromSchema(source schema.ISO) *ISO { + var hcloudISO ISO + hcloudISO.ID = source.ID + hcloudISO.Name = source.Name + hcloudISO.Description = source.Description + hcloudISO.Type = ISOType(source.Type) + var pHcloudArchitecture *Architecture + if source.Architecture != nil { + hcloudArchitecture := Architecture(*source.Architecture) + pHcloudArchitecture = &hcloudArchitecture + } + hcloudISO.Architecture = pHcloudArchitecture + hcloudISO.Deprecated = c.timeTimeToTimeTime(source.Deprecated) + hcloudISO.DeprecatableResource = c.schemaDeprecatableResourceToHcloudDeprecatableResource(source.DeprecatableResource) + return &hcloudISO +} +func (c *converterImpl) ImageFromSchema(source schema.Image) *Image { + var hcloudImage Image + hcloudImage.ID = source.ID + var xstring string + if source.Name != nil { + xstring = *source.Name + } + hcloudImage.Name = xstring + hcloudImage.Type = ImageType(source.Type) + hcloudImage.Status = ImageStatus(source.Status) + hcloudImage.Description = source.Description + var xfloat32 float32 + if source.ImageSize != nil { + xfloat32 = *source.ImageSize + } + hcloudImage.ImageSize = xfloat32 + hcloudImage.DiskSize = source.DiskSize + hcloudImage.Created = c.timeTimeToTimeTime(source.Created) + hcloudImage.CreatedFrom = c.pSchemaImageCreatedFromToPHcloudServer(source.CreatedFrom) + var pHcloudServer *Server + if source.BoundTo != nil { + hcloudServer := serverFromInt64(*source.BoundTo) + pHcloudServer = &hcloudServer + } + hcloudImage.BoundTo = pHcloudServer + hcloudImage.RapidDeploy = source.RapidDeploy + hcloudImage.OSFlavor = source.OSFlavor + var xstring2 string + if source.OSVersion != nil { + xstring2 = *source.OSVersion + } + hcloudImage.OSVersion = xstring2 + hcloudImage.Architecture = Architecture(source.Architecture) + hcloudImage.Protection = c.schemaImageProtectionToHcloudImageProtection(source.Protection) + hcloudImage.Deprecated = c.timeTimeToTimeTime(source.Deprecated) + hcloudImage.Labels = source.Labels + hcloudImage.Deleted = c.timeTimeToTimeTime(source.Deleted) + return &hcloudImage +} +func (c *converterImpl) LoadBalancerFromSchema(source schema.LoadBalancer) *LoadBalancer { + var hcloudLoadBalancer LoadBalancer + hcloudLoadBalancer.ID = source.ID + hcloudLoadBalancer.Name = source.Name + hcloudLoadBalancer.PublicNet = c.schemaLoadBalancerPublicNetToHcloudLoadBalancerPublicNet(source.PublicNet) + var hcloudLoadBalancerPrivateNetList []LoadBalancerPrivateNet + if source.PrivateNet != nil { + hcloudLoadBalancerPrivateNetList = make([]LoadBalancerPrivateNet, len(source.PrivateNet)) + for i := 0; i < len(source.PrivateNet); i++ { + hcloudLoadBalancerPrivateNetList[i] = c.schemaLoadBalancerPrivateNetToHcloudLoadBalancerPrivateNet(source.PrivateNet[i]) + } + } + hcloudLoadBalancer.PrivateNet = hcloudLoadBalancerPrivateNetList + hcloudLoadBalancer.Location = c.LocationFromSchema(source.Location) + hcloudLoadBalancer.LoadBalancerType = c.LoadBalancerTypeFromSchema(source.LoadBalancerType) + hcloudLoadBalancer.Algorithm = c.schemaLoadBalancerAlgorithmToHcloudLoadBalancerAlgorithm(source.Algorithm) + var hcloudLoadBalancerServiceList []LoadBalancerService + if source.Services != nil { + hcloudLoadBalancerServiceList = make([]LoadBalancerService, len(source.Services)) + for j := 0; j < len(source.Services); j++ { + hcloudLoadBalancerServiceList[j] = c.LoadBalancerServiceFromSchema(source.Services[j]) + } + } + hcloudLoadBalancer.Services = hcloudLoadBalancerServiceList + var hcloudLoadBalancerTargetList []LoadBalancerTarget + if source.Targets != nil { + hcloudLoadBalancerTargetList = make([]LoadBalancerTarget, len(source.Targets)) + for k := 0; k < len(source.Targets); k++ { + hcloudLoadBalancerTargetList[k] = c.LoadBalancerTargetFromSchema(source.Targets[k]) + } + } + hcloudLoadBalancer.Targets = hcloudLoadBalancerTargetList + hcloudLoadBalancer.Protection = c.schemaLoadBalancerProtectionToHcloudLoadBalancerProtection(source.Protection) + hcloudLoadBalancer.Labels = source.Labels + hcloudLoadBalancer.Created = c.timeTimeToTimeTime(source.Created) + hcloudLoadBalancer.IncludedTraffic = source.IncludedTraffic + var xuint64 uint64 + if source.OutgoingTraffic != nil { + xuint64 = *source.OutgoingTraffic + } + hcloudLoadBalancer.OutgoingTraffic = xuint64 + var xuint642 uint64 + if source.IngoingTraffic != nil { + xuint642 = *source.IngoingTraffic + } + hcloudLoadBalancer.IngoingTraffic = xuint642 + return &hcloudLoadBalancer +} +func (c *converterImpl) LoadBalancerMetricsFromSchema(source *schema.LoadBalancerGetMetricsResponse) (*LoadBalancerMetrics, error) { + var pHcloudLoadBalancerMetrics *LoadBalancerMetrics + if source != nil { + var hcloudLoadBalancerMetrics LoadBalancerMetrics + hcloudLoadBalancerMetrics.Start = c.timeTimeToTimeTime((*source).Metrics.Start) + hcloudLoadBalancerMetrics.End = c.timeTimeToTimeTime((*source).Metrics.End) + hcloudLoadBalancerMetrics.Step = (*source).Metrics.Step + mapStringHcloudLoadBalancerMetricsValueList := make(map[string][]LoadBalancerMetricsValue, len((*source).Metrics.TimeSeries)) + for key, value := range (*source).Metrics.TimeSeries { + hcloudLoadBalancerMetricsValueList, err := loadBalancerMetricsTimeSeriesFromSchema(value) + if err != nil { + return nil, err + } + mapStringHcloudLoadBalancerMetricsValueList[key] = hcloudLoadBalancerMetricsValueList + } + hcloudLoadBalancerMetrics.TimeSeries = mapStringHcloudLoadBalancerMetricsValueList + pHcloudLoadBalancerMetrics = &hcloudLoadBalancerMetrics + } + return pHcloudLoadBalancerMetrics, nil +} +func (c *converterImpl) LoadBalancerServiceFromSchema(source schema.LoadBalancerService) LoadBalancerService { + var hcloudLoadBalancerService LoadBalancerService + hcloudLoadBalancerService.Protocol = LoadBalancerServiceProtocol(source.Protocol) + hcloudLoadBalancerService.ListenPort = source.ListenPort + hcloudLoadBalancerService.DestinationPort = source.DestinationPort + hcloudLoadBalancerService.Proxyprotocol = source.Proxyprotocol + hcloudLoadBalancerService.HTTP = c.pSchemaLoadBalancerServiceHTTPToHcloudLoadBalancerServiceHTTP(source.HTTP) + hcloudLoadBalancerService.HealthCheck = c.LoadBalancerServiceHealthCheckFromSchema(source.HealthCheck) + return hcloudLoadBalancerService +} +func (c *converterImpl) LoadBalancerServiceHealthCheckFromSchema(source *schema.LoadBalancerServiceHealthCheck) LoadBalancerServiceHealthCheck { + var hcloudLoadBalancerServiceHealthCheck LoadBalancerServiceHealthCheck + if source != nil { + var hcloudLoadBalancerServiceHealthCheck2 LoadBalancerServiceHealthCheck + hcloudLoadBalancerServiceHealthCheck2.Protocol = LoadBalancerServiceProtocol((*source).Protocol) + hcloudLoadBalancerServiceHealthCheck2.Port = (*source).Port + hcloudLoadBalancerServiceHealthCheck2.Interval = durationFromIntSeconds((*source).Interval) + hcloudLoadBalancerServiceHealthCheck2.Timeout = durationFromIntSeconds((*source).Timeout) + hcloudLoadBalancerServiceHealthCheck2.Retries = (*source).Retries + hcloudLoadBalancerServiceHealthCheck2.HTTP = c.pSchemaLoadBalancerServiceHealthCheckHTTPToPHcloudLoadBalancerServiceHealthCheckHTTP((*source).HTTP) + hcloudLoadBalancerServiceHealthCheck = hcloudLoadBalancerServiceHealthCheck2 + } + return hcloudLoadBalancerServiceHealthCheck +} +func (c *converterImpl) LoadBalancerTargetFromSchema(source schema.LoadBalancerTarget) LoadBalancerTarget { + var hcloudLoadBalancerTarget LoadBalancerTarget + hcloudLoadBalancerTarget.Type = LoadBalancerTargetType(source.Type) + hcloudLoadBalancerTarget.Server = c.pSchemaLoadBalancerTargetServerToPHcloudLoadBalancerTargetServer(source.Server) + hcloudLoadBalancerTarget.LabelSelector = c.pSchemaLoadBalancerTargetLabelSelectorToPHcloudLoadBalancerTargetLabelSelector(source.LabelSelector) + hcloudLoadBalancerTarget.IP = c.pSchemaLoadBalancerTargetIPToPHcloudLoadBalancerTargetIP(source.IP) + var hcloudLoadBalancerTargetHealthStatusList []LoadBalancerTargetHealthStatus + if source.HealthStatus != nil { + hcloudLoadBalancerTargetHealthStatusList = make([]LoadBalancerTargetHealthStatus, len(source.HealthStatus)) + for i := 0; i < len(source.HealthStatus); i++ { + hcloudLoadBalancerTargetHealthStatusList[i] = c.LoadBalancerTargetHealthStatusFromSchema(source.HealthStatus[i]) + } + } + hcloudLoadBalancerTarget.HealthStatus = hcloudLoadBalancerTargetHealthStatusList + var hcloudLoadBalancerTargetList []LoadBalancerTarget + if source.Targets != nil { + hcloudLoadBalancerTargetList = make([]LoadBalancerTarget, len(source.Targets)) + for j := 0; j < len(source.Targets); j++ { + hcloudLoadBalancerTargetList[j] = c.LoadBalancerTargetFromSchema(source.Targets[j]) + } + } + hcloudLoadBalancerTarget.Targets = hcloudLoadBalancerTargetList + hcloudLoadBalancerTarget.UsePrivateIP = source.UsePrivateIP + return hcloudLoadBalancerTarget +} +func (c *converterImpl) LoadBalancerTargetHealthStatusFromSchema(source schema.LoadBalancerTargetHealthStatus) LoadBalancerTargetHealthStatus { + var hcloudLoadBalancerTargetHealthStatus LoadBalancerTargetHealthStatus + hcloudLoadBalancerTargetHealthStatus.ListenPort = source.ListenPort + hcloudLoadBalancerTargetHealthStatus.Status = LoadBalancerTargetHealthStatusStatus(source.Status) + return hcloudLoadBalancerTargetHealthStatus +} +func (c *converterImpl) LoadBalancerTargetServerFromSchema(source schema.LoadBalancerTargetServer) LoadBalancerTargetServer { + var hcloudLoadBalancerTargetServer LoadBalancerTargetServer + hcloudServer := serverFromInt64(source.ID) + hcloudLoadBalancerTargetServer.Server = &hcloudServer + return hcloudLoadBalancerTargetServer +} +func (c *converterImpl) LoadBalancerTypeFromSchema(source schema.LoadBalancerType) *LoadBalancerType { + var hcloudLoadBalancerType LoadBalancerType + hcloudLoadBalancerType.ID = source.ID + hcloudLoadBalancerType.Name = source.Name + hcloudLoadBalancerType.Description = source.Description + hcloudLoadBalancerType.MaxConnections = source.MaxConnections + hcloudLoadBalancerType.MaxServices = source.MaxServices + hcloudLoadBalancerType.MaxTargets = source.MaxTargets + hcloudLoadBalancerType.MaxAssignedCertificates = source.MaxAssignedCertificates + var hcloudLoadBalancerTypeLocationPricingList []LoadBalancerTypeLocationPricing + if source.Prices != nil { + hcloudLoadBalancerTypeLocationPricingList = make([]LoadBalancerTypeLocationPricing, len(source.Prices)) + for i := 0; i < len(source.Prices); i++ { + hcloudLoadBalancerTypeLocationPricingList[i] = c.LoadBalancerTypeLocationPricingFromSchema(source.Prices[i]) + } + } + hcloudLoadBalancerType.Pricings = hcloudLoadBalancerTypeLocationPricingList + return &hcloudLoadBalancerType +} +func (c *converterImpl) LoadBalancerTypeLocationPricingFromSchema(source schema.PricingLoadBalancerTypePrice) LoadBalancerTypeLocationPricing { + var hcloudLoadBalancerTypeLocationPricing LoadBalancerTypeLocationPricing + hcloudLocation := locationFromString(source.Location) + hcloudLoadBalancerTypeLocationPricing.Location = &hcloudLocation + hcloudLoadBalancerTypeLocationPricing.Hourly = c.PriceFromSchema(source.PriceHourly) + hcloudLoadBalancerTypeLocationPricing.Monthly = c.PriceFromSchema(source.PriceMonthly) + return hcloudLoadBalancerTypeLocationPricing +} +func (c *converterImpl) LocationFromSchema(source schema.Location) *Location { + var hcloudLocation Location + hcloudLocation.ID = source.ID + hcloudLocation.Name = source.Name + hcloudLocation.Description = source.Description + hcloudLocation.Country = source.Country + hcloudLocation.City = source.City + hcloudLocation.Latitude = source.Latitude + hcloudLocation.Longitude = source.Longitude + hcloudLocation.NetworkZone = NetworkZone(source.NetworkZone) + return &hcloudLocation +} +func (c *converterImpl) NetworkFromSchema(source schema.Network) *Network { + var hcloudNetwork Network + hcloudNetwork.ID = source.ID + hcloudNetwork.Name = source.Name + hcloudNetwork.Created = c.timeTimeToTimeTime(source.Created) + netIPNet := ipNetFromString(source.IPRange) + hcloudNetwork.IPRange = &netIPNet + var hcloudNetworkSubnetList []NetworkSubnet + if source.Subnets != nil { + hcloudNetworkSubnetList = make([]NetworkSubnet, len(source.Subnets)) + for i := 0; i < len(source.Subnets); i++ { + hcloudNetworkSubnetList[i] = c.NetworkSubnetFromSchema(source.Subnets[i]) + } + } + hcloudNetwork.Subnets = hcloudNetworkSubnetList + var hcloudNetworkRouteList []NetworkRoute + if source.Routes != nil { + hcloudNetworkRouteList = make([]NetworkRoute, len(source.Routes)) + for j := 0; j < len(source.Routes); j++ { + hcloudNetworkRouteList[j] = c.NetworkRouteFromSchema(source.Routes[j]) + } + } + hcloudNetwork.Routes = hcloudNetworkRouteList + var pHcloudServerList []*Server + if source.Servers != nil { + pHcloudServerList = make([]*Server, len(source.Servers)) + for k := 0; k < len(source.Servers); k++ { + hcloudServer := serverFromInt64(source.Servers[k]) + pHcloudServerList[k] = &hcloudServer + } + } + hcloudNetwork.Servers = pHcloudServerList + hcloudNetwork.Protection = c.schemaNetworkProtectionToHcloudNetworkProtection(source.Protection) + hcloudNetwork.Labels = source.Labels + hcloudNetwork.ExposeRoutesToVSwitch = source.ExposeRoutesToVSwitch + return &hcloudNetwork +} +func (c *converterImpl) NetworkRouteFromSchema(source schema.NetworkRoute) NetworkRoute { + var hcloudNetworkRoute NetworkRoute + netIPNet := ipNetFromString(source.Destination) + hcloudNetworkRoute.Destination = &netIPNet + hcloudNetworkRoute.Gateway = ipFromString(source.Gateway) + return hcloudNetworkRoute +} +func (c *converterImpl) NetworkSubnetFromSchema(source schema.NetworkSubnet) NetworkSubnet { + var hcloudNetworkSubnet NetworkSubnet + hcloudNetworkSubnet.Type = NetworkSubnetType(source.Type) + netIPNet := ipNetFromString(source.IPRange) + hcloudNetworkSubnet.IPRange = &netIPNet + hcloudNetworkSubnet.NetworkZone = NetworkZone(source.NetworkZone) + hcloudNetworkSubnet.Gateway = ipFromString(source.Gateway) + hcloudNetworkSubnet.VSwitchID = source.VSwitchID + return hcloudNetworkSubnet +} +func (c *converterImpl) PaginationFromSchema(source schema.MetaPagination) Pagination { + var hcloudPagination Pagination + hcloudPagination.Page = source.Page + hcloudPagination.PerPage = source.PerPage + hcloudPagination.PreviousPage = source.PreviousPage + hcloudPagination.NextPage = source.NextPage + hcloudPagination.LastPage = source.LastPage + hcloudPagination.TotalEntries = source.TotalEntries + return hcloudPagination +} +func (c *converterImpl) PlacementGroupFromSchema(source schema.PlacementGroup) *PlacementGroup { + var hcloudPlacementGroup PlacementGroup + hcloudPlacementGroup.ID = source.ID + hcloudPlacementGroup.Name = source.Name + hcloudPlacementGroup.Labels = source.Labels + hcloudPlacementGroup.Created = c.timeTimeToTimeTime(source.Created) + hcloudPlacementGroup.Servers = source.Servers + hcloudPlacementGroup.Type = PlacementGroupType(source.Type) + return &hcloudPlacementGroup +} +func (c *converterImpl) PriceFromSchema(source schema.Price) Price { + var hcloudPrice Price + hcloudPrice.Net = source.Net + hcloudPrice.Gross = source.Gross + return hcloudPrice +} +func (c *converterImpl) PricingFromSchema(source schema.Pricing) Pricing { + var hcloudPricing Pricing + hcloudPricing.Image = imagePricingFromSchema(source) + hcloudPricing.FloatingIP = floatingIPPricingFromSchema(source) + hcloudPricing.FloatingIPs = floatingIPTypePricingFromSchema(source) + hcloudPricing.PrimaryIPs = primaryIPPricingFromSchema(source) + hcloudPricing.Traffic = trafficPricingFromSchema(source) + hcloudPricing.ServerBackup = c.schemaPricingServerBackupToHcloudServerBackupPricing(source.ServerBackup) + hcloudPricing.ServerTypes = serverTypePricingFromSchema(source) + hcloudPricing.LoadBalancerTypes = loadBalancerTypePricingFromSchema(source) + hcloudPricing.Volume = volumePricingFromSchema(source) + return hcloudPricing +} +func (c *converterImpl) PrimaryIPFromSchema(source schema.PrimaryIP) *PrimaryIP { + var hcloudPrimaryIP PrimaryIP + hcloudPrimaryIP.ID = source.ID + hcloudPrimaryIP.IP = ipFromPrimaryIPSchema(source) + hcloudPrimaryIP.Network = networkFromPrimaryIPSchema(source) + hcloudPrimaryIP.Labels = source.Labels + hcloudPrimaryIP.Name = source.Name + hcloudPrimaryIP.Type = PrimaryIPType(source.Type) + hcloudPrimaryIP.Protection = c.schemaPrimaryIPProtectionToHcloudPrimaryIPProtection(source.Protection) + hcloudPrimaryIP.DNSPtr = mapFromPrimaryIPDNSPtrSchema(source.DNSPtr) + hcloudPrimaryIP.AssigneeID = source.AssigneeID + hcloudPrimaryIP.AssigneeType = source.AssigneeType + hcloudPrimaryIP.AutoDelete = source.AutoDelete + hcloudPrimaryIP.Blocked = source.Blocked + hcloudPrimaryIP.Created = c.timeTimeToTimeTime(source.Created) + hcloudPrimaryIP.Datacenter = c.DatacenterFromSchema(source.Datacenter) + return &hcloudPrimaryIP +} +func (c *converterImpl) SSHKeyFromSchema(source schema.SSHKey) *SSHKey { + var hcloudSSHKey SSHKey + hcloudSSHKey.ID = source.ID + hcloudSSHKey.Name = source.Name + hcloudSSHKey.Fingerprint = source.Fingerprint + hcloudSSHKey.PublicKey = source.PublicKey + hcloudSSHKey.Labels = source.Labels + hcloudSSHKey.Created = c.timeTimeToTimeTime(source.Created) + return &hcloudSSHKey +} +func (c *converterImpl) SchemaFromAction(source *Action) schema.Action { + var schemaAction schema.Action + if source != nil { + var schemaAction2 schema.Action + schemaAction2.ID = (*source).ID + schemaAction2.Status = string((*source).Status) + schemaAction2.Command = (*source).Command + schemaAction2.Progress = (*source).Progress + schemaAction2.Started = c.timeTimeToTimeTime((*source).Started) + schemaAction2.Finished = timeToTimePtr((*source).Finished) + schemaAction2.Error = schemaActionErrorFromAction((*source)) + var schemaActionResourceReferenceList []schema.ActionResourceReference + if (*source).Resources != nil { + schemaActionResourceReferenceList = make([]schema.ActionResourceReference, len((*source).Resources)) + for i := 0; i < len((*source).Resources); i++ { + schemaActionResourceReferenceList[i] = c.pHcloudActionResourceToSchemaActionResourceReference((*source).Resources[i]) + } + } + schemaAction2.Resources = schemaActionResourceReferenceList + schemaAction = schemaAction2 + } + return schemaAction +} +func (c *converterImpl) SchemaFromActions(source []*Action) []schema.Action { + var schemaActionList []schema.Action + if source != nil { + schemaActionList = make([]schema.Action, len(source)) + for i := 0; i < len(source); i++ { + schemaActionList[i] = c.SchemaFromAction(source[i]) + } + } + return schemaActionList +} +func (c *converterImpl) SchemaFromCertificate(source *Certificate) schema.Certificate { + var schemaCertificate schema.Certificate + if source != nil { + var schemaCertificate2 schema.Certificate + schemaCertificate2.ID = (*source).ID + schemaCertificate2.Name = (*source).Name + schemaCertificate2.Labels = (*source).Labels + schemaCertificate2.Type = string((*source).Type) + schemaCertificate2.Certificate = (*source).Certificate + schemaCertificate2.Created = c.timeTimeToTimeTime((*source).Created) + schemaCertificate2.NotValidBefore = c.timeTimeToTimeTime((*source).NotValidBefore) + schemaCertificate2.NotValidAfter = c.timeTimeToTimeTime((*source).NotValidAfter) + schemaCertificate2.DomainNames = (*source).DomainNames + schemaCertificate2.Fingerprint = (*source).Fingerprint + schemaCertificate2.Status = c.pHcloudCertificateStatusToPSchemaCertificateStatusRef((*source).Status) + var schemaCertificateUsedByRefList []schema.CertificateUsedByRef + if (*source).UsedBy != nil { + schemaCertificateUsedByRefList = make([]schema.CertificateUsedByRef, len((*source).UsedBy)) + for i := 0; i < len((*source).UsedBy); i++ { + schemaCertificateUsedByRefList[i] = c.hcloudCertificateUsedByRefToSchemaCertificateUsedByRef((*source).UsedBy[i]) + } + } + schemaCertificate2.UsedBy = schemaCertificateUsedByRefList + schemaCertificate = schemaCertificate2 + } + return schemaCertificate +} +func (c *converterImpl) SchemaFromDatacenter(source *Datacenter) schema.Datacenter { + var schemaDatacenter schema.Datacenter + if source != nil { + var schemaDatacenter2 schema.Datacenter + schemaDatacenter2.ID = (*source).ID + schemaDatacenter2.Name = (*source).Name + schemaDatacenter2.Description = (*source).Description + schemaDatacenter2.Location = c.SchemaFromLocation((*source).Location) + schemaDatacenter2.ServerTypes = c.hcloudDatacenterServerTypesToUnnamed((*source).ServerTypes) + schemaDatacenter = schemaDatacenter2 + } + return schemaDatacenter +} +func (c *converterImpl) SchemaFromDeprecation(source *DeprecationInfo) *schema.DeprecationInfo { + var pSchemaDeprecationInfo *schema.DeprecationInfo + if source != nil { + var schemaDeprecationInfo schema.DeprecationInfo + schemaDeprecationInfo.Announced = c.timeTimeToTimeTime((*source).Announced) + schemaDeprecationInfo.UnavailableAfter = c.timeTimeToTimeTime((*source).UnavailableAfter) + pSchemaDeprecationInfo = &schemaDeprecationInfo + } + return pSchemaDeprecationInfo +} +func (c *converterImpl) SchemaFromError(source Error) schema.Error { + var schemaError schema.Error + schemaError.Code = string(source.Code) + schemaError.Message = source.Message + schemaError.DetailsRaw = rawSchemaFromErrorDetails(source.Details) + schemaError.Details = schemaFromErrorDetails(source.Details) + return schemaError +} +func (c *converterImpl) SchemaFromFirewall(source *Firewall) schema.Firewall { + var schemaFirewall schema.Firewall + if source != nil { + var schemaFirewall2 schema.Firewall + schemaFirewall2.ID = (*source).ID + schemaFirewall2.Name = (*source).Name + schemaFirewall2.Labels = (*source).Labels + schemaFirewall2.Created = c.timeTimeToTimeTime((*source).Created) + var schemaFirewallRuleList []schema.FirewallRule + if (*source).Rules != nil { + schemaFirewallRuleList = make([]schema.FirewallRule, len((*source).Rules)) + for i := 0; i < len((*source).Rules); i++ { + schemaFirewallRuleList[i] = c.hcloudFirewallRuleToSchemaFirewallRule((*source).Rules[i]) + } + } + schemaFirewall2.Rules = schemaFirewallRuleList + var schemaFirewallResourceList []schema.FirewallResource + if (*source).AppliedTo != nil { + schemaFirewallResourceList = make([]schema.FirewallResource, len((*source).AppliedTo)) + for j := 0; j < len((*source).AppliedTo); j++ { + schemaFirewallResourceList[j] = c.SchemaFromFirewallResource((*source).AppliedTo[j]) + } + } + schemaFirewall2.AppliedTo = schemaFirewallResourceList + schemaFirewall = schemaFirewall2 + } + return schemaFirewall +} +func (c *converterImpl) SchemaFromFirewallCreateOpts(source FirewallCreateOpts) schema.FirewallCreateRequest { + var schemaFirewallCreateRequest schema.FirewallCreateRequest + schemaFirewallCreateRequest.Name = source.Name + schemaFirewallCreateRequest.Labels = stringMapToStringMapPtr(source.Labels) + var schemaFirewallRuleList []schema.FirewallRule + if source.Rules != nil { + schemaFirewallRuleList = make([]schema.FirewallRule, len(source.Rules)) + for i := 0; i < len(source.Rules); i++ { + schemaFirewallRuleList[i] = c.hcloudFirewallRuleToSchemaFirewallRule(source.Rules[i]) + } + } + schemaFirewallCreateRequest.Rules = schemaFirewallRuleList + var schemaFirewallResourceList []schema.FirewallResource + if source.ApplyTo != nil { + schemaFirewallResourceList = make([]schema.FirewallResource, len(source.ApplyTo)) + for j := 0; j < len(source.ApplyTo); j++ { + schemaFirewallResourceList[j] = c.SchemaFromFirewallResource(source.ApplyTo[j]) + } + } + schemaFirewallCreateRequest.ApplyTo = schemaFirewallResourceList + return schemaFirewallCreateRequest +} +func (c *converterImpl) SchemaFromFirewallResource(source FirewallResource) schema.FirewallResource { + var schemaFirewallResource schema.FirewallResource + schemaFirewallResource.Type = string(source.Type) + schemaFirewallResource.Server = c.pHcloudFirewallResourceServerToPSchemaFirewallResourceServer(source.Server) + schemaFirewallResource.LabelSelector = c.pHcloudFirewallResourceLabelSelectorToPSchemaFirewallResourceLabelSelector(source.LabelSelector) + return schemaFirewallResource +} +func (c *converterImpl) SchemaFromFirewallSetRulesOpts(source FirewallSetRulesOpts) schema.FirewallActionSetRulesRequest { + var schemaFirewallActionSetRulesRequest schema.FirewallActionSetRulesRequest + var schemaFirewallRuleList []schema.FirewallRule + if source.Rules != nil { + schemaFirewallRuleList = make([]schema.FirewallRule, len(source.Rules)) + for i := 0; i < len(source.Rules); i++ { + schemaFirewallRuleList[i] = c.hcloudFirewallRuleToSchemaFirewallRule(source.Rules[i]) + } + } + schemaFirewallActionSetRulesRequest.Rules = schemaFirewallRuleList + return schemaFirewallActionSetRulesRequest +} +func (c *converterImpl) SchemaFromFloatingIP(source *FloatingIP) schema.FloatingIP { + var schemaFloatingIP schema.FloatingIP + if source != nil { + var schemaFloatingIP2 schema.FloatingIP + schemaFloatingIP2.ID = (*source).ID + pString := (*source).Description + schemaFloatingIP2.Description = &pString + schemaFloatingIP2.Created = c.timeTimeToTimeTime((*source).Created) + schemaFloatingIP2.IP = floatingIPToIPString((*source)) + schemaFloatingIP2.Type = string((*source).Type) + schemaFloatingIP2.Server = c.pHcloudServerToPInt64((*source).Server) + schemaFloatingIP2.DNSPtr = floatingIPDNSPtrSchemaFromMap((*source).DNSPtr) + schemaFloatingIP2.HomeLocation = c.SchemaFromLocation((*source).HomeLocation) + schemaFloatingIP2.Blocked = (*source).Blocked + schemaFloatingIP2.Protection = c.hcloudFloatingIPProtectionToSchemaFloatingIPProtection((*source).Protection) + schemaFloatingIP2.Labels = (*source).Labels + schemaFloatingIP2.Name = (*source).Name + schemaFloatingIP = schemaFloatingIP2 + } + return schemaFloatingIP +} +func (c *converterImpl) SchemaFromISO(source *ISO) schema.ISO { + var schemaISO schema.ISO + if source != nil { + var schemaISO2 schema.ISO + schemaISO2.ID = (*source).ID + schemaISO2.Name = (*source).Name + schemaISO2.Description = (*source).Description + schemaISO2.Type = string((*source).Type) + var pString *string + if (*source).Architecture != nil { + xstring := string(*(*source).Architecture) + pString = &xstring + } + schemaISO2.Architecture = pString + schemaISO2.Deprecated = c.timeTimeToTimeTime((*source).Deprecated) + schemaISO2.DeprecatableResource = c.hcloudDeprecatableResourceToSchemaDeprecatableResource((*source).DeprecatableResource) + schemaISO = schemaISO2 + } + return schemaISO +} +func (c *converterImpl) SchemaFromImage(source *Image) schema.Image { + var schemaImage schema.Image + if source != nil { + var schemaImage2 schema.Image + schemaImage2.ID = (*source).ID + schemaImage2.Status = string((*source).Status) + schemaImage2.Type = string((*source).Type) + pString := (*source).Name + schemaImage2.Name = &pString + schemaImage2.Description = (*source).Description + pFloat32 := (*source).ImageSize + schemaImage2.ImageSize = &pFloat32 + schemaImage2.DiskSize = (*source).DiskSize + schemaImage2.Created = c.timeTimeToTimeTime((*source).Created) + schemaImage2.CreatedFrom = c.pHcloudServerToPSchemaImageCreatedFrom((*source).CreatedFrom) + schemaImage2.BoundTo = c.pHcloudServerToPInt64((*source).BoundTo) + schemaImage2.OSFlavor = (*source).OSFlavor + pString2 := (*source).OSVersion + schemaImage2.OSVersion = &pString2 + schemaImage2.Architecture = string((*source).Architecture) + schemaImage2.RapidDeploy = (*source).RapidDeploy + schemaImage2.Protection = c.hcloudImageProtectionToSchemaImageProtection((*source).Protection) + schemaImage2.Deprecated = c.timeTimeToTimeTime((*source).Deprecated) + schemaImage2.Deleted = c.timeTimeToTimeTime((*source).Deleted) + schemaImage2.Labels = (*source).Labels + schemaImage = schemaImage2 + } + return schemaImage +} +func (c *converterImpl) SchemaFromLoadBalancer(source *LoadBalancer) schema.LoadBalancer { + var schemaLoadBalancer schema.LoadBalancer + if source != nil { + var schemaLoadBalancer2 schema.LoadBalancer + schemaLoadBalancer2.ID = (*source).ID + schemaLoadBalancer2.Name = (*source).Name + schemaLoadBalancer2.PublicNet = c.hcloudLoadBalancerPublicNetToSchemaLoadBalancerPublicNet((*source).PublicNet) + var schemaLoadBalancerPrivateNetList []schema.LoadBalancerPrivateNet + if (*source).PrivateNet != nil { + schemaLoadBalancerPrivateNetList = make([]schema.LoadBalancerPrivateNet, len((*source).PrivateNet)) + for i := 0; i < len((*source).PrivateNet); i++ { + schemaLoadBalancerPrivateNetList[i] = c.hcloudLoadBalancerPrivateNetToSchemaLoadBalancerPrivateNet((*source).PrivateNet[i]) + } + } + schemaLoadBalancer2.PrivateNet = schemaLoadBalancerPrivateNetList + schemaLoadBalancer2.Location = c.SchemaFromLocation((*source).Location) + schemaLoadBalancer2.LoadBalancerType = c.SchemaFromLoadBalancerType((*source).LoadBalancerType) + schemaLoadBalancer2.Protection = c.hcloudLoadBalancerProtectionToSchemaLoadBalancerProtection((*source).Protection) + schemaLoadBalancer2.Labels = (*source).Labels + schemaLoadBalancer2.Created = c.timeTimeToTimeTime((*source).Created) + var schemaLoadBalancerServiceList []schema.LoadBalancerService + if (*source).Services != nil { + schemaLoadBalancerServiceList = make([]schema.LoadBalancerService, len((*source).Services)) + for j := 0; j < len((*source).Services); j++ { + schemaLoadBalancerServiceList[j] = c.SchemaFromLoadBalancerService((*source).Services[j]) + } + } + schemaLoadBalancer2.Services = schemaLoadBalancerServiceList + var schemaLoadBalancerTargetList []schema.LoadBalancerTarget + if (*source).Targets != nil { + schemaLoadBalancerTargetList = make([]schema.LoadBalancerTarget, len((*source).Targets)) + for k := 0; k < len((*source).Targets); k++ { + schemaLoadBalancerTargetList[k] = c.SchemaFromLoadBalancerTarget((*source).Targets[k]) + } + } + schemaLoadBalancer2.Targets = schemaLoadBalancerTargetList + schemaLoadBalancer2.Algorithm = c.hcloudLoadBalancerAlgorithmToSchemaLoadBalancerAlgorithm((*source).Algorithm) + schemaLoadBalancer2.IncludedTraffic = (*source).IncludedTraffic + pUint64 := (*source).OutgoingTraffic + schemaLoadBalancer2.OutgoingTraffic = &pUint64 + pUint642 := (*source).IngoingTraffic + schemaLoadBalancer2.IngoingTraffic = &pUint642 + schemaLoadBalancer = schemaLoadBalancer2 + } + return schemaLoadBalancer +} +func (c *converterImpl) SchemaFromLoadBalancerAddServiceOpts(source LoadBalancerAddServiceOpts) schema.LoadBalancerActionAddServiceRequest { + var schemaLoadBalancerActionAddServiceRequest schema.LoadBalancerActionAddServiceRequest + schemaLoadBalancerActionAddServiceRequest.Protocol = string(source.Protocol) + schemaLoadBalancerActionAddServiceRequest.ListenPort = source.ListenPort + schemaLoadBalancerActionAddServiceRequest.DestinationPort = source.DestinationPort + schemaLoadBalancerActionAddServiceRequest.Proxyprotocol = source.Proxyprotocol + schemaLoadBalancerActionAddServiceRequest.HTTP = c.pHcloudLoadBalancerAddServiceOptsHTTPToPSchemaLoadBalancerActionAddServiceRequestHTTP(source.HTTP) + schemaLoadBalancerActionAddServiceRequest.HealthCheck = c.pHcloudLoadBalancerAddServiceOptsHealthCheckToPSchemaLoadBalancerActionAddServiceRequestHealthCheck(source.HealthCheck) + return schemaLoadBalancerActionAddServiceRequest +} +func (c *converterImpl) SchemaFromLoadBalancerCreateOpts(source LoadBalancerCreateOpts) schema.LoadBalancerCreateRequest { + var schemaLoadBalancerCreateRequest schema.LoadBalancerCreateRequest + schemaLoadBalancerCreateRequest.Name = source.Name + schemaLoadBalancerCreateRequest.LoadBalancerType = anyFromLoadBalancerType(source.LoadBalancerType) + schemaLoadBalancerCreateRequest.Algorithm = c.pHcloudLoadBalancerAlgorithmToPSchemaLoadBalancerCreateRequestAlgorithm(source.Algorithm) + schemaLoadBalancerCreateRequest.Location = c.pHcloudLocationToPString(source.Location) + schemaLoadBalancerCreateRequest.NetworkZone = stringPtrFromNetworkZone(source.NetworkZone) + schemaLoadBalancerCreateRequest.Labels = stringMapToStringMapPtr(source.Labels) + var schemaLoadBalancerCreateRequestTargetList []schema.LoadBalancerCreateRequestTarget + if source.Targets != nil { + schemaLoadBalancerCreateRequestTargetList = make([]schema.LoadBalancerCreateRequestTarget, len(source.Targets)) + for i := 0; i < len(source.Targets); i++ { + schemaLoadBalancerCreateRequestTargetList[i] = c.hcloudLoadBalancerCreateOptsTargetToSchemaLoadBalancerCreateRequestTarget(source.Targets[i]) + } + } + schemaLoadBalancerCreateRequest.Targets = schemaLoadBalancerCreateRequestTargetList + var schemaLoadBalancerCreateRequestServiceList []schema.LoadBalancerCreateRequestService + if source.Services != nil { + schemaLoadBalancerCreateRequestServiceList = make([]schema.LoadBalancerCreateRequestService, len(source.Services)) + for j := 0; j < len(source.Services); j++ { + schemaLoadBalancerCreateRequestServiceList[j] = c.hcloudLoadBalancerCreateOptsServiceToSchemaLoadBalancerCreateRequestService(source.Services[j]) + } + } + schemaLoadBalancerCreateRequest.Services = schemaLoadBalancerCreateRequestServiceList + schemaLoadBalancerCreateRequest.PublicInterface = source.PublicInterface + schemaLoadBalancerCreateRequest.Network = c.pHcloudNetworkToPInt64(source.Network) + return schemaLoadBalancerCreateRequest +} +func (c *converterImpl) SchemaFromLoadBalancerCreateOptsTargetServer(source LoadBalancerCreateOptsTargetServer) schema.LoadBalancerCreateRequestTargetServer { + var schemaLoadBalancerCreateRequestTargetServer schema.LoadBalancerCreateRequestTargetServer + var pInt64 *int64 + if source.Server != nil { + pInt64 = &source.Server.ID + } + var xint64 int64 + if pInt64 != nil { + xint64 = *pInt64 + } + schemaLoadBalancerCreateRequestTargetServer.ID = xint64 + return schemaLoadBalancerCreateRequestTargetServer +} +func (c *converterImpl) SchemaFromLoadBalancerServerTarget(source LoadBalancerTargetServer) schema.LoadBalancerTargetServer { + var schemaLoadBalancerTargetServer schema.LoadBalancerTargetServer + schemaLoadBalancerTargetServer.ID = c.pHcloudServerToInt64(source.Server) + return schemaLoadBalancerTargetServer +} +func (c *converterImpl) SchemaFromLoadBalancerService(source LoadBalancerService) schema.LoadBalancerService { + var schemaLoadBalancerService schema.LoadBalancerService + schemaLoadBalancerService.Protocol = string(source.Protocol) + schemaLoadBalancerService.ListenPort = source.ListenPort + schemaLoadBalancerService.DestinationPort = source.DestinationPort + schemaLoadBalancerService.Proxyprotocol = source.Proxyprotocol + schemaLoadBalancerService.HTTP = c.hcloudLoadBalancerServiceHTTPToPSchemaLoadBalancerServiceHTTP(source.HTTP) + schemaLoadBalancerService.HealthCheck = c.SchemaFromLoadBalancerServiceHealthCheck(source.HealthCheck) + return schemaLoadBalancerService +} +func (c *converterImpl) SchemaFromLoadBalancerServiceHealthCheck(source LoadBalancerServiceHealthCheck) *schema.LoadBalancerServiceHealthCheck { + var schemaLoadBalancerServiceHealthCheck schema.LoadBalancerServiceHealthCheck + schemaLoadBalancerServiceHealthCheck.Protocol = string(source.Protocol) + schemaLoadBalancerServiceHealthCheck.Port = source.Port + schemaLoadBalancerServiceHealthCheck.Interval = intSecondsFromDuration(source.Interval) + schemaLoadBalancerServiceHealthCheck.Timeout = intSecondsFromDuration(source.Timeout) + schemaLoadBalancerServiceHealthCheck.Retries = source.Retries + schemaLoadBalancerServiceHealthCheck.HTTP = c.pHcloudLoadBalancerServiceHealthCheckHTTPToPSchemaLoadBalancerServiceHealthCheckHTTP(source.HTTP) + return &schemaLoadBalancerServiceHealthCheck +} +func (c *converterImpl) SchemaFromLoadBalancerTarget(source LoadBalancerTarget) schema.LoadBalancerTarget { + var schemaLoadBalancerTarget schema.LoadBalancerTarget + schemaLoadBalancerTarget.Type = string(source.Type) + schemaLoadBalancerTarget.Server = c.pHcloudLoadBalancerTargetServerToPSchemaLoadBalancerTargetServer(source.Server) + schemaLoadBalancerTarget.LabelSelector = c.pHcloudLoadBalancerTargetLabelSelectorToPSchemaLoadBalancerTargetLabelSelector(source.LabelSelector) + schemaLoadBalancerTarget.IP = c.pHcloudLoadBalancerTargetIPToPSchemaLoadBalancerTargetIP(source.IP) + var schemaLoadBalancerTargetHealthStatusList []schema.LoadBalancerTargetHealthStatus + if source.HealthStatus != nil { + schemaLoadBalancerTargetHealthStatusList = make([]schema.LoadBalancerTargetHealthStatus, len(source.HealthStatus)) + for i := 0; i < len(source.HealthStatus); i++ { + schemaLoadBalancerTargetHealthStatusList[i] = c.SchemaFromLoadBalancerTargetHealthStatus(source.HealthStatus[i]) + } + } + schemaLoadBalancerTarget.HealthStatus = schemaLoadBalancerTargetHealthStatusList + schemaLoadBalancerTarget.UsePrivateIP = source.UsePrivateIP + var schemaLoadBalancerTargetList []schema.LoadBalancerTarget + if source.Targets != nil { + schemaLoadBalancerTargetList = make([]schema.LoadBalancerTarget, len(source.Targets)) + for j := 0; j < len(source.Targets); j++ { + schemaLoadBalancerTargetList[j] = c.SchemaFromLoadBalancerTarget(source.Targets[j]) + } + } + schemaLoadBalancerTarget.Targets = schemaLoadBalancerTargetList + return schemaLoadBalancerTarget +} +func (c *converterImpl) SchemaFromLoadBalancerTargetHealthStatus(source LoadBalancerTargetHealthStatus) schema.LoadBalancerTargetHealthStatus { + var schemaLoadBalancerTargetHealthStatus schema.LoadBalancerTargetHealthStatus + schemaLoadBalancerTargetHealthStatus.ListenPort = source.ListenPort + schemaLoadBalancerTargetHealthStatus.Status = string(source.Status) + return schemaLoadBalancerTargetHealthStatus +} +func (c *converterImpl) SchemaFromLoadBalancerType(source *LoadBalancerType) schema.LoadBalancerType { + var schemaLoadBalancerType schema.LoadBalancerType + if source != nil { + var schemaLoadBalancerType2 schema.LoadBalancerType + schemaLoadBalancerType2.ID = (*source).ID + schemaLoadBalancerType2.Name = (*source).Name + schemaLoadBalancerType2.Description = (*source).Description + schemaLoadBalancerType2.MaxConnections = (*source).MaxConnections + schemaLoadBalancerType2.MaxServices = (*source).MaxServices + schemaLoadBalancerType2.MaxTargets = (*source).MaxTargets + schemaLoadBalancerType2.MaxAssignedCertificates = (*source).MaxAssignedCertificates + var schemaPricingLoadBalancerTypePriceList []schema.PricingLoadBalancerTypePrice + if (*source).Pricings != nil { + schemaPricingLoadBalancerTypePriceList = make([]schema.PricingLoadBalancerTypePrice, len((*source).Pricings)) + for i := 0; i < len((*source).Pricings); i++ { + schemaPricingLoadBalancerTypePriceList[i] = c.SchemaFromLoadBalancerTypeLocationPricing((*source).Pricings[i]) + } + } + schemaLoadBalancerType2.Prices = schemaPricingLoadBalancerTypePriceList + schemaLoadBalancerType = schemaLoadBalancerType2 + } + return schemaLoadBalancerType +} +func (c *converterImpl) SchemaFromLoadBalancerTypeLocationPricing(source LoadBalancerTypeLocationPricing) schema.PricingLoadBalancerTypePrice { + var schemaPricingLoadBalancerTypePrice schema.PricingLoadBalancerTypePrice + schemaPricingLoadBalancerTypePrice.Location = c.pHcloudLocationToString(source.Location) + schemaPricingLoadBalancerTypePrice.PriceHourly = c.hcloudPriceToSchemaPrice(source.Hourly) + schemaPricingLoadBalancerTypePrice.PriceMonthly = c.hcloudPriceToSchemaPrice(source.Monthly) + return schemaPricingLoadBalancerTypePrice +} +func (c *converterImpl) SchemaFromLoadBalancerUpdateServiceOpts(source LoadBalancerUpdateServiceOpts) schema.LoadBalancerActionUpdateServiceRequest { + var schemaLoadBalancerActionUpdateServiceRequest schema.LoadBalancerActionUpdateServiceRequest + schemaLoadBalancerActionUpdateServiceRequest.Protocol = stringPtrFromLoadBalancerServiceProtocol(source.Protocol) + schemaLoadBalancerActionUpdateServiceRequest.DestinationPort = source.DestinationPort + schemaLoadBalancerActionUpdateServiceRequest.Proxyprotocol = source.Proxyprotocol + schemaLoadBalancerActionUpdateServiceRequest.HTTP = c.pHcloudLoadBalancerUpdateServiceOptsHTTPToPSchemaLoadBalancerActionUpdateServiceRequestHTTP(source.HTTP) + schemaLoadBalancerActionUpdateServiceRequest.HealthCheck = c.pHcloudLoadBalancerUpdateServiceOptsHealthCheckToPSchemaLoadBalancerActionUpdateServiceRequestHealthCheck(source.HealthCheck) + return schemaLoadBalancerActionUpdateServiceRequest +} +func (c *converterImpl) SchemaFromLocation(source *Location) schema.Location { + var schemaLocation schema.Location + if source != nil { + var schemaLocation2 schema.Location + schemaLocation2.ID = (*source).ID + schemaLocation2.Name = (*source).Name + schemaLocation2.Description = (*source).Description + schemaLocation2.Country = (*source).Country + schemaLocation2.City = (*source).City + schemaLocation2.Latitude = (*source).Latitude + schemaLocation2.Longitude = (*source).Longitude + schemaLocation2.NetworkZone = string((*source).NetworkZone) + schemaLocation = schemaLocation2 + } + return schemaLocation +} +func (c *converterImpl) SchemaFromNetwork(source *Network) schema.Network { + var schemaNetwork schema.Network + if source != nil { + var schemaNetwork2 schema.Network + schemaNetwork2.ID = (*source).ID + schemaNetwork2.Name = (*source).Name + schemaNetwork2.Created = c.timeTimeToTimeTime((*source).Created) + schemaNetwork2.IPRange = c.pNetIPNetToString((*source).IPRange) + var schemaNetworkSubnetList []schema.NetworkSubnet + if (*source).Subnets != nil { + schemaNetworkSubnetList = make([]schema.NetworkSubnet, len((*source).Subnets)) + for i := 0; i < len((*source).Subnets); i++ { + schemaNetworkSubnetList[i] = c.SchemaFromNetworkSubnet((*source).Subnets[i]) + } + } + schemaNetwork2.Subnets = schemaNetworkSubnetList + var schemaNetworkRouteList []schema.NetworkRoute + if (*source).Routes != nil { + schemaNetworkRouteList = make([]schema.NetworkRoute, len((*source).Routes)) + for j := 0; j < len((*source).Routes); j++ { + schemaNetworkRouteList[j] = c.SchemaFromNetworkRoute((*source).Routes[j]) + } + } + schemaNetwork2.Routes = schemaNetworkRouteList + var int64List []int64 + if (*source).Servers != nil { + int64List = make([]int64, len((*source).Servers)) + for k := 0; k < len((*source).Servers); k++ { + int64List[k] = c.pHcloudServerToInt64((*source).Servers[k]) + } + } + schemaNetwork2.Servers = int64List + schemaNetwork2.Protection = c.hcloudNetworkProtectionToSchemaNetworkProtection((*source).Protection) + schemaNetwork2.Labels = (*source).Labels + schemaNetwork2.ExposeRoutesToVSwitch = (*source).ExposeRoutesToVSwitch + schemaNetwork = schemaNetwork2 + } + return schemaNetwork +} +func (c *converterImpl) SchemaFromNetworkRoute(source NetworkRoute) schema.NetworkRoute { + var schemaNetworkRoute schema.NetworkRoute + schemaNetworkRoute.Destination = c.pNetIPNetToString(source.Destination) + schemaNetworkRoute.Gateway = stringFromIP(source.Gateway) + return schemaNetworkRoute +} +func (c *converterImpl) SchemaFromNetworkSubnet(source NetworkSubnet) schema.NetworkSubnet { + var schemaNetworkSubnet schema.NetworkSubnet + schemaNetworkSubnet.Type = string(source.Type) + schemaNetworkSubnet.IPRange = c.pNetIPNetToString(source.IPRange) + schemaNetworkSubnet.NetworkZone = string(source.NetworkZone) + schemaNetworkSubnet.Gateway = stringFromIP(source.Gateway) + schemaNetworkSubnet.VSwitchID = source.VSwitchID + return schemaNetworkSubnet +} +func (c *converterImpl) SchemaFromPagination(source Pagination) schema.MetaPagination { + var schemaMetaPagination schema.MetaPagination + schemaMetaPagination.Page = source.Page + schemaMetaPagination.PerPage = source.PerPage + schemaMetaPagination.PreviousPage = source.PreviousPage + schemaMetaPagination.NextPage = source.NextPage + schemaMetaPagination.LastPage = source.LastPage + schemaMetaPagination.TotalEntries = source.TotalEntries + return schemaMetaPagination +} +func (c *converterImpl) SchemaFromPlacementGroup(source *PlacementGroup) schema.PlacementGroup { + var schemaPlacementGroup schema.PlacementGroup + if source != nil { + var schemaPlacementGroup2 schema.PlacementGroup + schemaPlacementGroup2.ID = (*source).ID + schemaPlacementGroup2.Name = (*source).Name + schemaPlacementGroup2.Labels = (*source).Labels + schemaPlacementGroup2.Created = c.timeTimeToTimeTime((*source).Created) + schemaPlacementGroup2.Servers = (*source).Servers + schemaPlacementGroup2.Type = string((*source).Type) + schemaPlacementGroup = schemaPlacementGroup2 + } + return schemaPlacementGroup +} +func (c *converterImpl) SchemaFromPlacementGroupCreateOpts(source PlacementGroupCreateOpts) schema.PlacementGroupCreateRequest { + var schemaPlacementGroupCreateRequest schema.PlacementGroupCreateRequest + schemaPlacementGroupCreateRequest.Name = source.Name + schemaPlacementGroupCreateRequest.Labels = stringMapToStringMapPtr(source.Labels) + schemaPlacementGroupCreateRequest.Type = string(source.Type) + return schemaPlacementGroupCreateRequest +} +func (c *converterImpl) SchemaFromPricing(source Pricing) schema.Pricing { + var schemaPricing schema.Pricing + schemaPricing.Currency = source.Image.PerGBMonth.Currency + schemaPricing.VATRate = source.Image.PerGBMonth.VATRate + schemaPricing.Image = c.schemaFromImagePricing(source.Image) + schemaPricing.FloatingIP = c.schemaFromFloatingIPPricing(source.FloatingIP) + var schemaPricingFloatingIPTypeList []schema.PricingFloatingIPType + if source.FloatingIPs != nil { + schemaPricingFloatingIPTypeList = make([]schema.PricingFloatingIPType, len(source.FloatingIPs)) + for i := 0; i < len(source.FloatingIPs); i++ { + schemaPricingFloatingIPTypeList[i] = c.schemaFromFloatingIPTypePricing(source.FloatingIPs[i]) + } + } + schemaPricing.FloatingIPs = schemaPricingFloatingIPTypeList + var schemaPricingPrimaryIPList []schema.PricingPrimaryIP + if source.PrimaryIPs != nil { + schemaPricingPrimaryIPList = make([]schema.PricingPrimaryIP, len(source.PrimaryIPs)) + for j := 0; j < len(source.PrimaryIPs); j++ { + schemaPricingPrimaryIPList[j] = c.schemaFromPrimaryIPPricing(source.PrimaryIPs[j]) + } + } + schemaPricing.PrimaryIPs = schemaPricingPrimaryIPList + schemaPricing.Traffic = c.schemaFromTrafficPricing(source.Traffic) + schemaPricing.ServerBackup = c.hcloudServerBackupPricingToSchemaPricingServerBackup(source.ServerBackup) + var schemaPricingServerTypeList []schema.PricingServerType + if source.ServerTypes != nil { + schemaPricingServerTypeList = make([]schema.PricingServerType, len(source.ServerTypes)) + for k := 0; k < len(source.ServerTypes); k++ { + schemaPricingServerTypeList[k] = c.schemaFromServerTypePricing(source.ServerTypes[k]) + } + } + schemaPricing.ServerTypes = schemaPricingServerTypeList + var schemaPricingLoadBalancerTypeList []schema.PricingLoadBalancerType + if source.LoadBalancerTypes != nil { + schemaPricingLoadBalancerTypeList = make([]schema.PricingLoadBalancerType, len(source.LoadBalancerTypes)) + for l := 0; l < len(source.LoadBalancerTypes); l++ { + schemaPricingLoadBalancerTypeList[l] = c.schemaFromLoadBalancerTypePricing(source.LoadBalancerTypes[l]) + } + } + schemaPricing.LoadBalancerTypes = schemaPricingLoadBalancerTypeList + schemaPricing.Volume = c.schemaFromVolumePricing(source.Volume) + return schemaPricing +} +func (c *converterImpl) SchemaFromPrimaryIP(source *PrimaryIP) schema.PrimaryIP { + var schemaPrimaryIP schema.PrimaryIP + if source != nil { + var schemaPrimaryIP2 schema.PrimaryIP + schemaPrimaryIP2.ID = (*source).ID + schemaPrimaryIP2.IP = primaryIPToIPString((*source)) + schemaPrimaryIP2.Labels = (*source).Labels + schemaPrimaryIP2.Name = (*source).Name + schemaPrimaryIP2.Type = string((*source).Type) + schemaPrimaryIP2.Protection = c.hcloudPrimaryIPProtectionToSchemaPrimaryIPProtection((*source).Protection) + schemaPrimaryIP2.DNSPtr = primaryIPDNSPtrSchemaFromMap((*source).DNSPtr) + schemaPrimaryIP2.AssigneeID = (*source).AssigneeID + schemaPrimaryIP2.AssigneeType = (*source).AssigneeType + schemaPrimaryIP2.AutoDelete = (*source).AutoDelete + schemaPrimaryIP2.Blocked = (*source).Blocked + schemaPrimaryIP2.Created = c.timeTimeToTimeTime((*source).Created) + schemaPrimaryIP2.Datacenter = c.SchemaFromDatacenter((*source).Datacenter) + schemaPrimaryIP = schemaPrimaryIP2 + } + return schemaPrimaryIP +} +func (c *converterImpl) SchemaFromSSHKey(source *SSHKey) schema.SSHKey { + var schemaSSHKey schema.SSHKey + if source != nil { + var schemaSSHKey2 schema.SSHKey + schemaSSHKey2.ID = (*source).ID + schemaSSHKey2.Name = (*source).Name + schemaSSHKey2.Fingerprint = (*source).Fingerprint + schemaSSHKey2.PublicKey = (*source).PublicKey + schemaSSHKey2.Labels = (*source).Labels + schemaSSHKey2.Created = c.timeTimeToTimeTime((*source).Created) + schemaSSHKey = schemaSSHKey2 + } + return schemaSSHKey +} +func (c *converterImpl) SchemaFromServer(source *Server) schema.Server { + var schemaServer schema.Server + if source != nil { + var schemaServer2 schema.Server + schemaServer2.ID = (*source).ID + schemaServer2.Name = (*source).Name + schemaServer2.Status = string((*source).Status) + schemaServer2.Created = c.timeTimeToTimeTime((*source).Created) + schemaServer2.PublicNet = c.SchemaFromServerPublicNet((*source).PublicNet) + var schemaServerPrivateNetList []schema.ServerPrivateNet + if (*source).PrivateNet != nil { + schemaServerPrivateNetList = make([]schema.ServerPrivateNet, len((*source).PrivateNet)) + for i := 0; i < len((*source).PrivateNet); i++ { + schemaServerPrivateNetList[i] = c.SchemaFromServerPrivateNet((*source).PrivateNet[i]) + } + } + schemaServer2.PrivateNet = schemaServerPrivateNetList + schemaServer2.ServerType = c.SchemaFromServerType((*source).ServerType) + schemaServer2.IncludedTraffic = (*source).IncludedTraffic + schemaServer2.OutgoingTraffic = mapZeroUint64ToNil((*source).OutgoingTraffic) + schemaServer2.IngoingTraffic = mapZeroUint64ToNil((*source).IngoingTraffic) + schemaServer2.BackupWindow = mapEmptyStringToNil((*source).BackupWindow) + schemaServer2.RescueEnabled = (*source).RescueEnabled + schemaServer2.ISO = c.pHcloudISOToPSchemaISO((*source).ISO) + schemaServer2.Locked = (*source).Locked + schemaServer2.Datacenter = c.SchemaFromDatacenter((*source).Datacenter) + schemaServer2.Image = c.pHcloudImageToPSchemaImage((*source).Image) + schemaServer2.Protection = c.hcloudServerProtectionToSchemaServerProtection((*source).Protection) + schemaServer2.Labels = (*source).Labels + var int64List []int64 + if (*source).Volumes != nil { + int64List = make([]int64, len((*source).Volumes)) + for j := 0; j < len((*source).Volumes); j++ { + int64List[j] = int64FromVolume((*source).Volumes[j]) + } + } + schemaServer2.Volumes = int64List + schemaServer2.PrimaryDiskSize = (*source).PrimaryDiskSize + schemaServer2.PlacementGroup = c.pHcloudPlacementGroupToPSchemaPlacementGroup((*source).PlacementGroup) + schemaServer = schemaServer2 + } + return schemaServer +} +func (c *converterImpl) SchemaFromServerPrivateNet(source ServerPrivateNet) schema.ServerPrivateNet { + var schemaServerPrivateNet schema.ServerPrivateNet + schemaServerPrivateNet.Network = c.pHcloudNetworkToInt64(source.Network) + schemaServerPrivateNet.IP = stringFromIP(source.IP) + var stringList []string + if source.Aliases != nil { + stringList = make([]string, len(source.Aliases)) + for i := 0; i < len(source.Aliases); i++ { + stringList[i] = stringFromIP(source.Aliases[i]) + } + } + schemaServerPrivateNet.AliasIPs = stringList + schemaServerPrivateNet.MACAddress = source.MACAddress + return schemaServerPrivateNet +} +func (c *converterImpl) SchemaFromServerPublicNet(source ServerPublicNet) schema.ServerPublicNet { + var schemaServerPublicNet schema.ServerPublicNet + schemaServerPublicNet.IPv4 = c.SchemaFromServerPublicNetIPv4(source.IPv4) + schemaServerPublicNet.IPv6 = c.SchemaFromServerPublicNetIPv6(source.IPv6) + var int64List []int64 + if source.FloatingIPs != nil { + int64List = make([]int64, len(source.FloatingIPs)) + for i := 0; i < len(source.FloatingIPs); i++ { + int64List[i] = int64FromFloatingIP(source.FloatingIPs[i]) + } + } + schemaServerPublicNet.FloatingIPs = int64List + var schemaServerFirewallList []schema.ServerFirewall + if source.Firewalls != nil { + schemaServerFirewallList = make([]schema.ServerFirewall, len(source.Firewalls)) + for j := 0; j < len(source.Firewalls); j++ { + schemaServerFirewallList[j] = serverFirewallSchemaFromFirewallStatus(source.Firewalls[j]) + } + } + schemaServerPublicNet.Firewalls = schemaServerFirewallList + return schemaServerPublicNet +} +func (c *converterImpl) SchemaFromServerPublicNetIPv4(source ServerPublicNetIPv4) schema.ServerPublicNetIPv4 { + var schemaServerPublicNetIPv4 schema.ServerPublicNetIPv4 + schemaServerPublicNetIPv4.ID = source.ID + schemaServerPublicNetIPv4.IP = stringFromIP(source.IP) + schemaServerPublicNetIPv4.Blocked = source.Blocked + schemaServerPublicNetIPv4.DNSPtr = source.DNSPtr + return schemaServerPublicNetIPv4 +} +func (c *converterImpl) SchemaFromServerPublicNetIPv6(source ServerPublicNetIPv6) schema.ServerPublicNetIPv6 { + var schemaServerPublicNetIPv6 schema.ServerPublicNetIPv6 + schemaServerPublicNetIPv6.ID = source.ID + schemaServerPublicNetIPv6.IP = c.pNetIPNetToString(source.Network) + schemaServerPublicNetIPv6.Blocked = source.Blocked + schemaServerPublicNetIPv6.DNSPtr = serverPublicNetIPv6DNSPtrSchemaFromMap(source.DNSPtr) + return schemaServerPublicNetIPv6 +} +func (c *converterImpl) SchemaFromServerType(source *ServerType) schema.ServerType { + var schemaServerType schema.ServerType + if source != nil { + var schemaServerType2 schema.ServerType + schemaServerType2.ID = (*source).ID + schemaServerType2.Name = (*source).Name + schemaServerType2.Description = (*source).Description + schemaServerType2.Cores = (*source).Cores + schemaServerType2.Memory = (*source).Memory + schemaServerType2.Disk = (*source).Disk + schemaServerType2.StorageType = string((*source).StorageType) + schemaServerType2.CPUType = string((*source).CPUType) + schemaServerType2.Architecture = string((*source).Architecture) + schemaServerType2.IncludedTraffic = (*source).IncludedTraffic + var schemaPricingServerTypePriceList []schema.PricingServerTypePrice + if (*source).Pricings != nil { + schemaPricingServerTypePriceList = make([]schema.PricingServerTypePrice, len((*source).Pricings)) + for i := 0; i < len((*source).Pricings); i++ { + schemaPricingServerTypePriceList[i] = c.schemaFromServerTypeLocationPricing((*source).Pricings[i]) + } + } + schemaServerType2.Prices = schemaPricingServerTypePriceList + schemaServerType2.DeprecatableResource = c.hcloudDeprecatableResourceToSchemaDeprecatableResource((*source).DeprecatableResource) + schemaServerType = schemaServerType2 + } + return schemaServerType +} +func (c *converterImpl) SchemaFromVolume(source *Volume) schema.Volume { + var schemaVolume schema.Volume + if source != nil { + var schemaVolume2 schema.Volume + schemaVolume2.ID = (*source).ID + schemaVolume2.Name = (*source).Name + schemaVolume2.Server = c.pHcloudServerToPInt64((*source).Server) + schemaVolume2.Status = string((*source).Status) + schemaVolume2.Location = c.SchemaFromLocation((*source).Location) + schemaVolume2.Size = (*source).Size + schemaVolume2.Protection = c.hcloudVolumeProtectionToSchemaVolumeProtection((*source).Protection) + schemaVolume2.Labels = (*source).Labels + schemaVolume2.LinuxDevice = (*source).LinuxDevice + schemaVolume2.Created = c.timeTimeToTimeTime((*source).Created) + schemaVolume = schemaVolume2 + } + return schemaVolume +} +func (c *converterImpl) ServerFromSchema(source schema.Server) *Server { + var hcloudServer Server + hcloudServer.ID = source.ID + hcloudServer.Name = source.Name + hcloudServer.Status = ServerStatus(source.Status) + hcloudServer.Created = c.timeTimeToTimeTime(source.Created) + hcloudServer.PublicNet = c.ServerPublicNetFromSchema(source.PublicNet) + var hcloudServerPrivateNetList []ServerPrivateNet + if source.PrivateNet != nil { + hcloudServerPrivateNetList = make([]ServerPrivateNet, len(source.PrivateNet)) + for i := 0; i < len(source.PrivateNet); i++ { + hcloudServerPrivateNetList[i] = c.ServerPrivateNetFromSchema(source.PrivateNet[i]) + } + } + hcloudServer.PrivateNet = hcloudServerPrivateNetList + hcloudServer.ServerType = c.ServerTypeFromSchema(source.ServerType) + hcloudServer.Datacenter = c.DatacenterFromSchema(source.Datacenter) + hcloudServer.IncludedTraffic = source.IncludedTraffic + var xuint64 uint64 + if source.OutgoingTraffic != nil { + xuint64 = *source.OutgoingTraffic + } + hcloudServer.OutgoingTraffic = xuint64 + var xuint642 uint64 + if source.IngoingTraffic != nil { + xuint642 = *source.IngoingTraffic + } + hcloudServer.IngoingTraffic = xuint642 + var xstring string + if source.BackupWindow != nil { + xstring = *source.BackupWindow + } + hcloudServer.BackupWindow = xstring + hcloudServer.RescueEnabled = source.RescueEnabled + hcloudServer.Locked = source.Locked + hcloudServer.ISO = c.pSchemaISOToPHcloudISO(source.ISO) + hcloudServer.Image = c.pSchemaImageToPHcloudImage(source.Image) + hcloudServer.Protection = c.schemaServerProtectionToHcloudServerProtection(source.Protection) + hcloudServer.Labels = source.Labels + var pHcloudVolumeList []*Volume + if source.Volumes != nil { + pHcloudVolumeList = make([]*Volume, len(source.Volumes)) + for j := 0; j < len(source.Volumes); j++ { + pHcloudVolumeList[j] = volumeFromInt64(source.Volumes[j]) + } + } + hcloudServer.Volumes = pHcloudVolumeList + hcloudServer.PrimaryDiskSize = source.PrimaryDiskSize + hcloudServer.PlacementGroup = c.pSchemaPlacementGroupToPHcloudPlacementGroup(source.PlacementGroup) + return &hcloudServer +} +func (c *converterImpl) ServerMetricsFromSchema(source *schema.ServerGetMetricsResponse) (*ServerMetrics, error) { + var pHcloudServerMetrics *ServerMetrics + if source != nil { + var hcloudServerMetrics ServerMetrics + hcloudServerMetrics.Start = c.timeTimeToTimeTime((*source).Metrics.Start) + hcloudServerMetrics.End = c.timeTimeToTimeTime((*source).Metrics.End) + hcloudServerMetrics.Step = (*source).Metrics.Step + mapStringHcloudServerMetricsValueList := make(map[string][]ServerMetricsValue, len((*source).Metrics.TimeSeries)) + for key, value := range (*source).Metrics.TimeSeries { + hcloudServerMetricsValueList, err := serverMetricsTimeSeriesFromSchema(value) + if err != nil { + return nil, err + } + mapStringHcloudServerMetricsValueList[key] = hcloudServerMetricsValueList + } + hcloudServerMetrics.TimeSeries = mapStringHcloudServerMetricsValueList + pHcloudServerMetrics = &hcloudServerMetrics + } + return pHcloudServerMetrics, nil +} +func (c *converterImpl) ServerPrivateNetFromSchema(source schema.ServerPrivateNet) ServerPrivateNet { + var hcloudServerPrivateNet ServerPrivateNet + hcloudNetwork := networkFromInt64(source.Network) + hcloudServerPrivateNet.Network = &hcloudNetwork + hcloudServerPrivateNet.IP = ipFromString(source.IP) + var netIPList []net.IP + if source.AliasIPs != nil { + netIPList = make([]net.IP, len(source.AliasIPs)) + for i := 0; i < len(source.AliasIPs); i++ { + netIPList[i] = ipFromString(source.AliasIPs[i]) + } + } + hcloudServerPrivateNet.Aliases = netIPList + hcloudServerPrivateNet.MACAddress = source.MACAddress + return hcloudServerPrivateNet +} +func (c *converterImpl) ServerPublicNetFromSchema(source schema.ServerPublicNet) ServerPublicNet { + var hcloudServerPublicNet ServerPublicNet + hcloudServerPublicNet.IPv4 = c.ServerPublicNetIPv4FromSchema(source.IPv4) + hcloudServerPublicNet.IPv6 = c.ServerPublicNetIPv6FromSchema(source.IPv6) + var pHcloudFloatingIPList []*FloatingIP + if source.FloatingIPs != nil { + pHcloudFloatingIPList = make([]*FloatingIP, len(source.FloatingIPs)) + for i := 0; i < len(source.FloatingIPs); i++ { + pHcloudFloatingIPList[i] = floatingIPFromInt64(source.FloatingIPs[i]) + } + } + hcloudServerPublicNet.FloatingIPs = pHcloudFloatingIPList + var pHcloudServerFirewallStatusList []*ServerFirewallStatus + if source.Firewalls != nil { + pHcloudServerFirewallStatusList = make([]*ServerFirewallStatus, len(source.Firewalls)) + for j := 0; j < len(source.Firewalls); j++ { + pHcloudServerFirewallStatusList[j] = firewallStatusFromSchemaServerFirewall(source.Firewalls[j]) + } + } + hcloudServerPublicNet.Firewalls = pHcloudServerFirewallStatusList + return hcloudServerPublicNet +} +func (c *converterImpl) ServerPublicNetIPv4FromSchema(source schema.ServerPublicNetIPv4) ServerPublicNetIPv4 { + var hcloudServerPublicNetIPv4 ServerPublicNetIPv4 + hcloudServerPublicNetIPv4.ID = source.ID + hcloudServerPublicNetIPv4.IP = ipFromString(source.IP) + hcloudServerPublicNetIPv4.Blocked = source.Blocked + hcloudServerPublicNetIPv4.DNSPtr = source.DNSPtr + return hcloudServerPublicNetIPv4 +} +func (c *converterImpl) ServerPublicNetIPv6FromSchema(source schema.ServerPublicNetIPv6) ServerPublicNetIPv6 { + var hcloudServerPublicNetIPv6 ServerPublicNetIPv6 + hcloudServerPublicNetIPv6.ID = source.ID + hcloudServerPublicNetIPv6.IP = ipFromServerPublicNetIPv6Schema(source) + hcloudServerPublicNetIPv6.Network = ipNetFromServerPublicNetIPv6Schema(source) + hcloudServerPublicNetIPv6.Blocked = source.Blocked + hcloudServerPublicNetIPv6.DNSPtr = mapFromServerPublicNetIPv6DNSPtrSchema(source.DNSPtr) + return hcloudServerPublicNetIPv6 +} +func (c *converterImpl) ServerTypeFromSchema(source schema.ServerType) *ServerType { + var hcloudServerType ServerType + hcloudServerType.ID = source.ID + hcloudServerType.Name = source.Name + hcloudServerType.Description = source.Description + hcloudServerType.Cores = source.Cores + hcloudServerType.Memory = source.Memory + hcloudServerType.Disk = source.Disk + hcloudServerType.StorageType = StorageType(source.StorageType) + hcloudServerType.CPUType = CPUType(source.CPUType) + hcloudServerType.Architecture = Architecture(source.Architecture) + hcloudServerType.IncludedTraffic = source.IncludedTraffic + var hcloudServerTypeLocationPricingList []ServerTypeLocationPricing + if source.Prices != nil { + hcloudServerTypeLocationPricingList = make([]ServerTypeLocationPricing, len(source.Prices)) + for i := 0; i < len(source.Prices); i++ { + hcloudServerTypeLocationPricingList[i] = c.serverTypePricingFromSchema(source.Prices[i]) + } + } + hcloudServerType.Pricings = hcloudServerTypeLocationPricingList + hcloudServerType.DeprecatableResource = c.schemaDeprecatableResourceToHcloudDeprecatableResource(source.DeprecatableResource) + return &hcloudServerType +} +func (c *converterImpl) VolumeFromSchema(source schema.Volume) *Volume { + var hcloudVolume Volume + hcloudVolume.ID = source.ID + hcloudVolume.Name = source.Name + hcloudVolume.Status = VolumeStatus(source.Status) + var pHcloudServer *Server + if source.Server != nil { + hcloudServer := serverFromInt64(*source.Server) + pHcloudServer = &hcloudServer + } + hcloudVolume.Server = pHcloudServer + hcloudVolume.Location = c.LocationFromSchema(source.Location) + hcloudVolume.Size = source.Size + hcloudVolume.Protection = c.schemaVolumeProtectionToHcloudVolumeProtection(source.Protection) + hcloudVolume.Labels = source.Labels + hcloudVolume.LinuxDevice = source.LinuxDevice + hcloudVolume.Created = c.timeTimeToTimeTime(source.Created) + return &hcloudVolume +} +func (c *converterImpl) hcloudCertificateStatusTypeToString(source CertificateStatusType) string { + return string(source) +} +func (c *converterImpl) hcloudCertificateUsedByRefToSchemaCertificateUsedByRef(source CertificateUsedByRef) schema.CertificateUsedByRef { + var schemaCertificateUsedByRef schema.CertificateUsedByRef + schemaCertificateUsedByRef.ID = source.ID + schemaCertificateUsedByRef.Type = string(source.Type) + return schemaCertificateUsedByRef +} +func (c *converterImpl) hcloudDatacenterServerTypesToUnnamed(source DatacenterServerTypes) struct { + Supported []int64 `json:"supported"` + Available []int64 `json:"available"` +} { + var unnamed struct { + Supported []int64 `json:"supported"` + Available []int64 `json:"available"` + } + var int64List []int64 + if source.Supported != nil { + int64List = make([]int64, len(source.Supported)) + for i := 0; i < len(source.Supported); i++ { + int64List[i] = int64FromServerType(source.Supported[i]) + } + } + unnamed.Supported = int64List + var int64List2 []int64 + if source.Available != nil { + int64List2 = make([]int64, len(source.Available)) + for j := 0; j < len(source.Available); j++ { + int64List2[j] = int64FromServerType(source.Available[j]) + } + } + unnamed.Available = int64List2 + return unnamed +} +func (c *converterImpl) hcloudDeprecatableResourceToSchemaDeprecatableResource(source DeprecatableResource) schema.DeprecatableResource { + var schemaDeprecatableResource schema.DeprecatableResource + schemaDeprecatableResource.Deprecation = c.SchemaFromDeprecation(source.Deprecation) + return schemaDeprecatableResource +} +func (c *converterImpl) hcloudFirewallRuleToSchemaFirewallRule(source FirewallRule) schema.FirewallRule { + var schemaFirewallRule schema.FirewallRule + schemaFirewallRule.Direction = string(source.Direction) + var stringList []string + if source.SourceIPs != nil { + stringList = make([]string, len(source.SourceIPs)) + for i := 0; i < len(source.SourceIPs); i++ { + stringList[i] = stringFromIPNet(source.SourceIPs[i]) + } + } + schemaFirewallRule.SourceIPs = stringList + var stringList2 []string + if source.DestinationIPs != nil { + stringList2 = make([]string, len(source.DestinationIPs)) + for j := 0; j < len(source.DestinationIPs); j++ { + stringList2[j] = stringFromIPNet(source.DestinationIPs[j]) + } + } + schemaFirewallRule.DestinationIPs = stringList2 + schemaFirewallRule.Protocol = string(source.Protocol) + schemaFirewallRule.Port = source.Port + schemaFirewallRule.Description = source.Description + return schemaFirewallRule +} +func (c *converterImpl) hcloudFloatingIPProtectionToSchemaFloatingIPProtection(source FloatingIPProtection) schema.FloatingIPProtection { + var schemaFloatingIPProtection schema.FloatingIPProtection + schemaFloatingIPProtection.Delete = source.Delete + return schemaFloatingIPProtection +} +func (c *converterImpl) hcloudImageProtectionToSchemaImageProtection(source ImageProtection) schema.ImageProtection { + var schemaImageProtection schema.ImageProtection + schemaImageProtection.Delete = source.Delete + return schemaImageProtection +} +func (c *converterImpl) hcloudLoadBalancerAlgorithmToSchemaLoadBalancerAlgorithm(source LoadBalancerAlgorithm) schema.LoadBalancerAlgorithm { + var schemaLoadBalancerAlgorithm schema.LoadBalancerAlgorithm + schemaLoadBalancerAlgorithm.Type = string(source.Type) + return schemaLoadBalancerAlgorithm +} +func (c *converterImpl) hcloudLoadBalancerCreateOptsServiceToSchemaLoadBalancerCreateRequestService(source LoadBalancerCreateOptsService) schema.LoadBalancerCreateRequestService { + var schemaLoadBalancerCreateRequestService schema.LoadBalancerCreateRequestService + schemaLoadBalancerCreateRequestService.Protocol = string(source.Protocol) + schemaLoadBalancerCreateRequestService.ListenPort = source.ListenPort + schemaLoadBalancerCreateRequestService.DestinationPort = source.DestinationPort + schemaLoadBalancerCreateRequestService.Proxyprotocol = source.Proxyprotocol + schemaLoadBalancerCreateRequestService.HTTP = c.pHcloudLoadBalancerCreateOptsServiceHTTPToPSchemaLoadBalancerCreateRequestServiceHTTP(source.HTTP) + schemaLoadBalancerCreateRequestService.HealthCheck = c.pHcloudLoadBalancerCreateOptsServiceHealthCheckToPSchemaLoadBalancerCreateRequestServiceHealthCheck(source.HealthCheck) + return schemaLoadBalancerCreateRequestService +} +func (c *converterImpl) hcloudLoadBalancerCreateOptsTargetToSchemaLoadBalancerCreateRequestTarget(source LoadBalancerCreateOptsTarget) schema.LoadBalancerCreateRequestTarget { + var schemaLoadBalancerCreateRequestTarget schema.LoadBalancerCreateRequestTarget + schemaLoadBalancerCreateRequestTarget.Type = string(source.Type) + schemaLoadBalancerCreateRequestTarget.Server = schemaFromLoadBalancerCreateOptsTargetServer(source.Server) + schemaLoadBalancerCreateRequestTarget.LabelSelector = schemaFromLoadBalancerCreateOptsTargetLabelSelector(source.LabelSelector) + schemaLoadBalancerCreateRequestTarget.IP = schemaFromLoadBalancerCreateOptsTargetIP(source.IP) + schemaLoadBalancerCreateRequestTarget.UsePrivateIP = source.UsePrivateIP + return schemaLoadBalancerCreateRequestTarget +} +func (c *converterImpl) hcloudLoadBalancerPrivateNetToSchemaLoadBalancerPrivateNet(source LoadBalancerPrivateNet) schema.LoadBalancerPrivateNet { + var schemaLoadBalancerPrivateNet schema.LoadBalancerPrivateNet + schemaLoadBalancerPrivateNet.Network = c.pHcloudNetworkToInt64(source.Network) + schemaLoadBalancerPrivateNet.IP = stringFromIP(source.IP) + return schemaLoadBalancerPrivateNet +} +func (c *converterImpl) hcloudLoadBalancerProtectionToSchemaLoadBalancerProtection(source LoadBalancerProtection) schema.LoadBalancerProtection { + var schemaLoadBalancerProtection schema.LoadBalancerProtection + schemaLoadBalancerProtection.Delete = source.Delete + return schemaLoadBalancerProtection +} +func (c *converterImpl) hcloudLoadBalancerPublicNetIPv4ToSchemaLoadBalancerPublicNetIPv4(source LoadBalancerPublicNetIPv4) schema.LoadBalancerPublicNetIPv4 { + var schemaLoadBalancerPublicNetIPv4 schema.LoadBalancerPublicNetIPv4 + schemaLoadBalancerPublicNetIPv4.IP = stringFromIP(source.IP) + schemaLoadBalancerPublicNetIPv4.DNSPtr = source.DNSPtr + return schemaLoadBalancerPublicNetIPv4 +} +func (c *converterImpl) hcloudLoadBalancerPublicNetIPv6ToSchemaLoadBalancerPublicNetIPv6(source LoadBalancerPublicNetIPv6) schema.LoadBalancerPublicNetIPv6 { + var schemaLoadBalancerPublicNetIPv6 schema.LoadBalancerPublicNetIPv6 + schemaLoadBalancerPublicNetIPv6.IP = stringFromIP(source.IP) + schemaLoadBalancerPublicNetIPv6.DNSPtr = source.DNSPtr + return schemaLoadBalancerPublicNetIPv6 +} +func (c *converterImpl) hcloudLoadBalancerPublicNetToSchemaLoadBalancerPublicNet(source LoadBalancerPublicNet) schema.LoadBalancerPublicNet { + var schemaLoadBalancerPublicNet schema.LoadBalancerPublicNet + schemaLoadBalancerPublicNet.Enabled = source.Enabled + schemaLoadBalancerPublicNet.IPv4 = c.hcloudLoadBalancerPublicNetIPv4ToSchemaLoadBalancerPublicNetIPv4(source.IPv4) + schemaLoadBalancerPublicNet.IPv6 = c.hcloudLoadBalancerPublicNetIPv6ToSchemaLoadBalancerPublicNetIPv6(source.IPv6) + return schemaLoadBalancerPublicNet +} +func (c *converterImpl) hcloudLoadBalancerServiceHTTPToPSchemaLoadBalancerServiceHTTP(source LoadBalancerServiceHTTP) *schema.LoadBalancerServiceHTTP { + var schemaLoadBalancerServiceHTTP schema.LoadBalancerServiceHTTP + schemaLoadBalancerServiceHTTP.CookieName = source.CookieName + schemaLoadBalancerServiceHTTP.CookieLifetime = intSecondsFromDuration(source.CookieLifetime) + var int64List []int64 + if source.Certificates != nil { + int64List = make([]int64, len(source.Certificates)) + for i := 0; i < len(source.Certificates); i++ { + int64List[i] = int64FromCertificate(source.Certificates[i]) + } + } + schemaLoadBalancerServiceHTTP.Certificates = int64List + schemaLoadBalancerServiceHTTP.RedirectHTTP = source.RedirectHTTP + schemaLoadBalancerServiceHTTP.StickySessions = source.StickySessions + return &schemaLoadBalancerServiceHTTP +} +func (c *converterImpl) hcloudNetworkProtectionToSchemaNetworkProtection(source NetworkProtection) schema.NetworkProtection { + var schemaNetworkProtection schema.NetworkProtection + schemaNetworkProtection.Delete = source.Delete + return schemaNetworkProtection +} +func (c *converterImpl) hcloudPriceToSchemaPrice(source Price) schema.Price { + var schemaPrice schema.Price + schemaPrice.Net = source.Net + schemaPrice.Gross = source.Gross + return schemaPrice +} +func (c *converterImpl) hcloudPrimaryIPPriceToSchemaPrice(source PrimaryIPPrice) schema.Price { + var schemaPrice schema.Price + schemaPrice.Net = source.Net + schemaPrice.Gross = source.Gross + return schemaPrice +} +func (c *converterImpl) hcloudPrimaryIPProtectionToSchemaPrimaryIPProtection(source PrimaryIPProtection) schema.PrimaryIPProtection { + var schemaPrimaryIPProtection schema.PrimaryIPProtection + schemaPrimaryIPProtection.Delete = source.Delete + return schemaPrimaryIPProtection +} +func (c *converterImpl) hcloudServerBackupPricingToSchemaPricingServerBackup(source ServerBackupPricing) schema.PricingServerBackup { + var schemaPricingServerBackup schema.PricingServerBackup + schemaPricingServerBackup.Percentage = source.Percentage + return schemaPricingServerBackup +} +func (c *converterImpl) hcloudServerProtectionToSchemaServerProtection(source ServerProtection) schema.ServerProtection { + var schemaServerProtection schema.ServerProtection + schemaServerProtection.Delete = source.Delete + schemaServerProtection.Rebuild = source.Rebuild + return schemaServerProtection +} +func (c *converterImpl) hcloudVolumeProtectionToSchemaVolumeProtection(source VolumeProtection) schema.VolumeProtection { + var schemaVolumeProtection schema.VolumeProtection + schemaVolumeProtection.Delete = source.Delete + return schemaVolumeProtection +} +func (c *converterImpl) pHcloudActionResourceToSchemaActionResourceReference(source *ActionResource) schema.ActionResourceReference { + var schemaActionResourceReference schema.ActionResourceReference + if source != nil { + var schemaActionResourceReference2 schema.ActionResourceReference + schemaActionResourceReference2.ID = (*source).ID + schemaActionResourceReference2.Type = string((*source).Type) + schemaActionResourceReference = schemaActionResourceReference2 + } + return schemaActionResourceReference +} +func (c *converterImpl) pHcloudCertificateStatusToPSchemaCertificateStatusRef(source *CertificateStatus) *schema.CertificateStatusRef { + var pSchemaCertificateStatusRef *schema.CertificateStatusRef + if source != nil { + var schemaCertificateStatusRef schema.CertificateStatusRef + schemaCertificateStatusRef.Issuance = c.hcloudCertificateStatusTypeToString((*source).Issuance) + schemaCertificateStatusRef.Renewal = c.hcloudCertificateStatusTypeToString((*source).Renewal) + schemaCertificateStatusRef.Error = c.pHcloudErrorToPSchemaError((*source).Error) + pSchemaCertificateStatusRef = &schemaCertificateStatusRef + } + return pSchemaCertificateStatusRef +} +func (c *converterImpl) pHcloudErrorToPSchemaError(source *Error) *schema.Error { + var pSchemaError *schema.Error + if source != nil { + schemaError := c.SchemaFromError((*source)) + pSchemaError = &schemaError + } + return pSchemaError +} +func (c *converterImpl) pHcloudFirewallResourceLabelSelectorToPSchemaFirewallResourceLabelSelector(source *FirewallResourceLabelSelector) *schema.FirewallResourceLabelSelector { + var pSchemaFirewallResourceLabelSelector *schema.FirewallResourceLabelSelector + if source != nil { + var schemaFirewallResourceLabelSelector schema.FirewallResourceLabelSelector + schemaFirewallResourceLabelSelector.Selector = (*source).Selector + pSchemaFirewallResourceLabelSelector = &schemaFirewallResourceLabelSelector + } + return pSchemaFirewallResourceLabelSelector +} +func (c *converterImpl) pHcloudFirewallResourceServerToPSchemaFirewallResourceServer(source *FirewallResourceServer) *schema.FirewallResourceServer { + var pSchemaFirewallResourceServer *schema.FirewallResourceServer + if source != nil { + var schemaFirewallResourceServer schema.FirewallResourceServer + schemaFirewallResourceServer.ID = (*source).ID + pSchemaFirewallResourceServer = &schemaFirewallResourceServer + } + return pSchemaFirewallResourceServer +} +func (c *converterImpl) pHcloudISOToPSchemaISO(source *ISO) *schema.ISO { + var pSchemaISO *schema.ISO + if source != nil { + var schemaISO schema.ISO + schemaISO.ID = (*source).ID + schemaISO.Name = (*source).Name + schemaISO.Description = (*source).Description + schemaISO.Type = string((*source).Type) + var pString *string + if (*source).Architecture != nil { + xstring := string(*(*source).Architecture) + pString = &xstring + } + schemaISO.Architecture = pString + schemaISO.Deprecated = c.timeTimeToTimeTime((*source).Deprecated) + schemaISO.DeprecatableResource = c.hcloudDeprecatableResourceToSchemaDeprecatableResource((*source).DeprecatableResource) + pSchemaISO = &schemaISO + } + return pSchemaISO +} +func (c *converterImpl) pHcloudImageToPSchemaImage(source *Image) *schema.Image { + var pSchemaImage *schema.Image + if source != nil { + var schemaImage schema.Image + schemaImage.ID = (*source).ID + schemaImage.Status = string((*source).Status) + schemaImage.Type = string((*source).Type) + pString := (*source).Name + schemaImage.Name = &pString + schemaImage.Description = (*source).Description + pFloat32 := (*source).ImageSize + schemaImage.ImageSize = &pFloat32 + schemaImage.DiskSize = (*source).DiskSize + schemaImage.Created = c.timeTimeToTimeTime((*source).Created) + schemaImage.CreatedFrom = c.pHcloudServerToPSchemaImageCreatedFrom((*source).CreatedFrom) + schemaImage.BoundTo = c.pHcloudServerToPInt64((*source).BoundTo) + schemaImage.OSFlavor = (*source).OSFlavor + pString2 := (*source).OSVersion + schemaImage.OSVersion = &pString2 + schemaImage.Architecture = string((*source).Architecture) + schemaImage.RapidDeploy = (*source).RapidDeploy + schemaImage.Protection = c.hcloudImageProtectionToSchemaImageProtection((*source).Protection) + schemaImage.Deprecated = c.timeTimeToTimeTime((*source).Deprecated) + schemaImage.Deleted = c.timeTimeToTimeTime((*source).Deleted) + schemaImage.Labels = (*source).Labels + pSchemaImage = &schemaImage + } + return pSchemaImage +} +func (c *converterImpl) pHcloudLoadBalancerAddServiceOptsHTTPToPSchemaLoadBalancerActionAddServiceRequestHTTP(source *LoadBalancerAddServiceOptsHTTP) *schema.LoadBalancerActionAddServiceRequestHTTP { + var pSchemaLoadBalancerActionAddServiceRequestHTTP *schema.LoadBalancerActionAddServiceRequestHTTP + if source != nil { + var schemaLoadBalancerActionAddServiceRequestHTTP schema.LoadBalancerActionAddServiceRequestHTTP + schemaLoadBalancerActionAddServiceRequestHTTP.CookieName = (*source).CookieName + var pInt *int + if (*source).CookieLifetime != nil { + xint := intSecondsFromDuration(*(*source).CookieLifetime) + pInt = &xint + } + schemaLoadBalancerActionAddServiceRequestHTTP.CookieLifetime = pInt + var int64List []int64 + if (*source).Certificates != nil { + int64List = make([]int64, len((*source).Certificates)) + for i := 0; i < len((*source).Certificates); i++ { + int64List[i] = int64FromCertificate((*source).Certificates[i]) + } + } + schemaLoadBalancerActionAddServiceRequestHTTP.Certificates = &int64List + schemaLoadBalancerActionAddServiceRequestHTTP.RedirectHTTP = (*source).RedirectHTTP + schemaLoadBalancerActionAddServiceRequestHTTP.StickySessions = (*source).StickySessions + pSchemaLoadBalancerActionAddServiceRequestHTTP = &schemaLoadBalancerActionAddServiceRequestHTTP + } + return pSchemaLoadBalancerActionAddServiceRequestHTTP +} +func (c *converterImpl) pHcloudLoadBalancerAddServiceOptsHealthCheckHTTPToPSchemaLoadBalancerActionAddServiceRequestHealthCheckHTTP(source *LoadBalancerAddServiceOptsHealthCheckHTTP) *schema.LoadBalancerActionAddServiceRequestHealthCheckHTTP { + var pSchemaLoadBalancerActionAddServiceRequestHealthCheckHTTP *schema.LoadBalancerActionAddServiceRequestHealthCheckHTTP + if source != nil { + var schemaLoadBalancerActionAddServiceRequestHealthCheckHTTP schema.LoadBalancerActionAddServiceRequestHealthCheckHTTP + schemaLoadBalancerActionAddServiceRequestHealthCheckHTTP.Domain = (*source).Domain + schemaLoadBalancerActionAddServiceRequestHealthCheckHTTP.Path = (*source).Path + schemaLoadBalancerActionAddServiceRequestHealthCheckHTTP.Response = (*source).Response + schemaLoadBalancerActionAddServiceRequestHealthCheckHTTP.StatusCodes = &(*source).StatusCodes + schemaLoadBalancerActionAddServiceRequestHealthCheckHTTP.TLS = (*source).TLS + pSchemaLoadBalancerActionAddServiceRequestHealthCheckHTTP = &schemaLoadBalancerActionAddServiceRequestHealthCheckHTTP + } + return pSchemaLoadBalancerActionAddServiceRequestHealthCheckHTTP +} +func (c *converterImpl) pHcloudLoadBalancerAddServiceOptsHealthCheckToPSchemaLoadBalancerActionAddServiceRequestHealthCheck(source *LoadBalancerAddServiceOptsHealthCheck) *schema.LoadBalancerActionAddServiceRequestHealthCheck { + var pSchemaLoadBalancerActionAddServiceRequestHealthCheck *schema.LoadBalancerActionAddServiceRequestHealthCheck + if source != nil { + var schemaLoadBalancerActionAddServiceRequestHealthCheck schema.LoadBalancerActionAddServiceRequestHealthCheck + schemaLoadBalancerActionAddServiceRequestHealthCheck.Protocol = string((*source).Protocol) + schemaLoadBalancerActionAddServiceRequestHealthCheck.Port = (*source).Port + var pInt *int + if (*source).Interval != nil { + xint := intSecondsFromDuration(*(*source).Interval) + pInt = &xint + } + schemaLoadBalancerActionAddServiceRequestHealthCheck.Interval = pInt + var pInt2 *int + if (*source).Timeout != nil { + xint2 := intSecondsFromDuration(*(*source).Timeout) + pInt2 = &xint2 + } + schemaLoadBalancerActionAddServiceRequestHealthCheck.Timeout = pInt2 + schemaLoadBalancerActionAddServiceRequestHealthCheck.Retries = (*source).Retries + schemaLoadBalancerActionAddServiceRequestHealthCheck.HTTP = c.pHcloudLoadBalancerAddServiceOptsHealthCheckHTTPToPSchemaLoadBalancerActionAddServiceRequestHealthCheckHTTP((*source).HTTP) + pSchemaLoadBalancerActionAddServiceRequestHealthCheck = &schemaLoadBalancerActionAddServiceRequestHealthCheck + } + return pSchemaLoadBalancerActionAddServiceRequestHealthCheck +} +func (c *converterImpl) pHcloudLoadBalancerAlgorithmToPSchemaLoadBalancerCreateRequestAlgorithm(source *LoadBalancerAlgorithm) *schema.LoadBalancerCreateRequestAlgorithm { + var pSchemaLoadBalancerCreateRequestAlgorithm *schema.LoadBalancerCreateRequestAlgorithm + if source != nil { + var schemaLoadBalancerCreateRequestAlgorithm schema.LoadBalancerCreateRequestAlgorithm + schemaLoadBalancerCreateRequestAlgorithm.Type = string((*source).Type) + pSchemaLoadBalancerCreateRequestAlgorithm = &schemaLoadBalancerCreateRequestAlgorithm + } + return pSchemaLoadBalancerCreateRequestAlgorithm +} +func (c *converterImpl) pHcloudLoadBalancerCreateOptsServiceHTTPToPSchemaLoadBalancerCreateRequestServiceHTTP(source *LoadBalancerCreateOptsServiceHTTP) *schema.LoadBalancerCreateRequestServiceHTTP { + var pSchemaLoadBalancerCreateRequestServiceHTTP *schema.LoadBalancerCreateRequestServiceHTTP + if source != nil { + var schemaLoadBalancerCreateRequestServiceHTTP schema.LoadBalancerCreateRequestServiceHTTP + schemaLoadBalancerCreateRequestServiceHTTP.CookieName = (*source).CookieName + var pInt *int + if (*source).CookieLifetime != nil { + xint := intSecondsFromDuration(*(*source).CookieLifetime) + pInt = &xint + } + schemaLoadBalancerCreateRequestServiceHTTP.CookieLifetime = pInt + var int64List []int64 + if (*source).Certificates != nil { + int64List = make([]int64, len((*source).Certificates)) + for i := 0; i < len((*source).Certificates); i++ { + int64List[i] = int64FromCertificate((*source).Certificates[i]) + } + } + schemaLoadBalancerCreateRequestServiceHTTP.Certificates = &int64List + schemaLoadBalancerCreateRequestServiceHTTP.RedirectHTTP = (*source).RedirectHTTP + schemaLoadBalancerCreateRequestServiceHTTP.StickySessions = (*source).StickySessions + pSchemaLoadBalancerCreateRequestServiceHTTP = &schemaLoadBalancerCreateRequestServiceHTTP + } + return pSchemaLoadBalancerCreateRequestServiceHTTP +} +func (c *converterImpl) pHcloudLoadBalancerCreateOptsServiceHealthCheckHTTPToPSchemaLoadBalancerCreateRequestServiceHealthCheckHTTP(source *LoadBalancerCreateOptsServiceHealthCheckHTTP) *schema.LoadBalancerCreateRequestServiceHealthCheckHTTP { + var pSchemaLoadBalancerCreateRequestServiceHealthCheckHTTP *schema.LoadBalancerCreateRequestServiceHealthCheckHTTP + if source != nil { + var schemaLoadBalancerCreateRequestServiceHealthCheckHTTP schema.LoadBalancerCreateRequestServiceHealthCheckHTTP + schemaLoadBalancerCreateRequestServiceHealthCheckHTTP.Domain = (*source).Domain + schemaLoadBalancerCreateRequestServiceHealthCheckHTTP.Path = (*source).Path + schemaLoadBalancerCreateRequestServiceHealthCheckHTTP.Response = (*source).Response + schemaLoadBalancerCreateRequestServiceHealthCheckHTTP.StatusCodes = &(*source).StatusCodes + schemaLoadBalancerCreateRequestServiceHealthCheckHTTP.TLS = (*source).TLS + pSchemaLoadBalancerCreateRequestServiceHealthCheckHTTP = &schemaLoadBalancerCreateRequestServiceHealthCheckHTTP + } + return pSchemaLoadBalancerCreateRequestServiceHealthCheckHTTP +} +func (c *converterImpl) pHcloudLoadBalancerCreateOptsServiceHealthCheckToPSchemaLoadBalancerCreateRequestServiceHealthCheck(source *LoadBalancerCreateOptsServiceHealthCheck) *schema.LoadBalancerCreateRequestServiceHealthCheck { + var pSchemaLoadBalancerCreateRequestServiceHealthCheck *schema.LoadBalancerCreateRequestServiceHealthCheck + if source != nil { + var schemaLoadBalancerCreateRequestServiceHealthCheck schema.LoadBalancerCreateRequestServiceHealthCheck + schemaLoadBalancerCreateRequestServiceHealthCheck.Protocol = string((*source).Protocol) + schemaLoadBalancerCreateRequestServiceHealthCheck.Port = (*source).Port + var pInt *int + if (*source).Interval != nil { + xint := intSecondsFromDuration(*(*source).Interval) + pInt = &xint + } + schemaLoadBalancerCreateRequestServiceHealthCheck.Interval = pInt + var pInt2 *int + if (*source).Timeout != nil { + xint2 := intSecondsFromDuration(*(*source).Timeout) + pInt2 = &xint2 + } + schemaLoadBalancerCreateRequestServiceHealthCheck.Timeout = pInt2 + schemaLoadBalancerCreateRequestServiceHealthCheck.Retries = (*source).Retries + schemaLoadBalancerCreateRequestServiceHealthCheck.HTTP = c.pHcloudLoadBalancerCreateOptsServiceHealthCheckHTTPToPSchemaLoadBalancerCreateRequestServiceHealthCheckHTTP((*source).HTTP) + pSchemaLoadBalancerCreateRequestServiceHealthCheck = &schemaLoadBalancerCreateRequestServiceHealthCheck + } + return pSchemaLoadBalancerCreateRequestServiceHealthCheck +} +func (c *converterImpl) pHcloudLoadBalancerServiceHealthCheckHTTPToPSchemaLoadBalancerServiceHealthCheckHTTP(source *LoadBalancerServiceHealthCheckHTTP) *schema.LoadBalancerServiceHealthCheckHTTP { + var pSchemaLoadBalancerServiceHealthCheckHTTP *schema.LoadBalancerServiceHealthCheckHTTP + if source != nil { + var schemaLoadBalancerServiceHealthCheckHTTP schema.LoadBalancerServiceHealthCheckHTTP + schemaLoadBalancerServiceHealthCheckHTTP.Domain = (*source).Domain + schemaLoadBalancerServiceHealthCheckHTTP.Path = (*source).Path + schemaLoadBalancerServiceHealthCheckHTTP.Response = (*source).Response + schemaLoadBalancerServiceHealthCheckHTTP.StatusCodes = (*source).StatusCodes + schemaLoadBalancerServiceHealthCheckHTTP.TLS = (*source).TLS + pSchemaLoadBalancerServiceHealthCheckHTTP = &schemaLoadBalancerServiceHealthCheckHTTP + } + return pSchemaLoadBalancerServiceHealthCheckHTTP +} +func (c *converterImpl) pHcloudLoadBalancerTargetIPToPSchemaLoadBalancerTargetIP(source *LoadBalancerTargetIP) *schema.LoadBalancerTargetIP { + var pSchemaLoadBalancerTargetIP *schema.LoadBalancerTargetIP + if source != nil { + var schemaLoadBalancerTargetIP schema.LoadBalancerTargetIP + schemaLoadBalancerTargetIP.IP = (*source).IP + pSchemaLoadBalancerTargetIP = &schemaLoadBalancerTargetIP + } + return pSchemaLoadBalancerTargetIP +} +func (c *converterImpl) pHcloudLoadBalancerTargetLabelSelectorToPSchemaLoadBalancerTargetLabelSelector(source *LoadBalancerTargetLabelSelector) *schema.LoadBalancerTargetLabelSelector { + var pSchemaLoadBalancerTargetLabelSelector *schema.LoadBalancerTargetLabelSelector + if source != nil { + var schemaLoadBalancerTargetLabelSelector schema.LoadBalancerTargetLabelSelector + schemaLoadBalancerTargetLabelSelector.Selector = (*source).Selector + pSchemaLoadBalancerTargetLabelSelector = &schemaLoadBalancerTargetLabelSelector + } + return pSchemaLoadBalancerTargetLabelSelector +} +func (c *converterImpl) pHcloudLoadBalancerTargetServerToPSchemaLoadBalancerTargetServer(source *LoadBalancerTargetServer) *schema.LoadBalancerTargetServer { + var pSchemaLoadBalancerTargetServer *schema.LoadBalancerTargetServer + if source != nil { + schemaLoadBalancerTargetServer := c.SchemaFromLoadBalancerServerTarget((*source)) + pSchemaLoadBalancerTargetServer = &schemaLoadBalancerTargetServer + } + return pSchemaLoadBalancerTargetServer +} +func (c *converterImpl) pHcloudLoadBalancerUpdateServiceOptsHTTPToPSchemaLoadBalancerActionUpdateServiceRequestHTTP(source *LoadBalancerUpdateServiceOptsHTTP) *schema.LoadBalancerActionUpdateServiceRequestHTTP { + var pSchemaLoadBalancerActionUpdateServiceRequestHTTP *schema.LoadBalancerActionUpdateServiceRequestHTTP + if source != nil { + var schemaLoadBalancerActionUpdateServiceRequestHTTP schema.LoadBalancerActionUpdateServiceRequestHTTP + schemaLoadBalancerActionUpdateServiceRequestHTTP.CookieName = (*source).CookieName + var pInt *int + if (*source).CookieLifetime != nil { + xint := intSecondsFromDuration(*(*source).CookieLifetime) + pInt = &xint + } + schemaLoadBalancerActionUpdateServiceRequestHTTP.CookieLifetime = pInt + var int64List []int64 + if (*source).Certificates != nil { + int64List = make([]int64, len((*source).Certificates)) + for i := 0; i < len((*source).Certificates); i++ { + int64List[i] = int64FromCertificate((*source).Certificates[i]) + } + } + schemaLoadBalancerActionUpdateServiceRequestHTTP.Certificates = &int64List + schemaLoadBalancerActionUpdateServiceRequestHTTP.RedirectHTTP = (*source).RedirectHTTP + schemaLoadBalancerActionUpdateServiceRequestHTTP.StickySessions = (*source).StickySessions + pSchemaLoadBalancerActionUpdateServiceRequestHTTP = &schemaLoadBalancerActionUpdateServiceRequestHTTP + } + return pSchemaLoadBalancerActionUpdateServiceRequestHTTP +} +func (c *converterImpl) pHcloudLoadBalancerUpdateServiceOptsHealthCheckHTTPToPSchemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP(source *LoadBalancerUpdateServiceOptsHealthCheckHTTP) *schema.LoadBalancerActionUpdateServiceRequestHealthCheckHTTP { + var pSchemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP *schema.LoadBalancerActionUpdateServiceRequestHealthCheckHTTP + if source != nil { + var schemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP schema.LoadBalancerActionUpdateServiceRequestHealthCheckHTTP + schemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP.Domain = (*source).Domain + schemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP.Path = (*source).Path + schemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP.Response = (*source).Response + schemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP.StatusCodes = &(*source).StatusCodes + schemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP.TLS = (*source).TLS + pSchemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP = &schemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP + } + return pSchemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP +} +func (c *converterImpl) pHcloudLoadBalancerUpdateServiceOptsHealthCheckToPSchemaLoadBalancerActionUpdateServiceRequestHealthCheck(source *LoadBalancerUpdateServiceOptsHealthCheck) *schema.LoadBalancerActionUpdateServiceRequestHealthCheck { + var pSchemaLoadBalancerActionUpdateServiceRequestHealthCheck *schema.LoadBalancerActionUpdateServiceRequestHealthCheck + if source != nil { + var schemaLoadBalancerActionUpdateServiceRequestHealthCheck schema.LoadBalancerActionUpdateServiceRequestHealthCheck + schemaLoadBalancerActionUpdateServiceRequestHealthCheck.Protocol = stringPtrFromLoadBalancerServiceProtocol((*source).Protocol) + schemaLoadBalancerActionUpdateServiceRequestHealthCheck.Port = (*source).Port + var pInt *int + if (*source).Interval != nil { + xint := intSecondsFromDuration(*(*source).Interval) + pInt = &xint + } + schemaLoadBalancerActionUpdateServiceRequestHealthCheck.Interval = pInt + var pInt2 *int + if (*source).Timeout != nil { + xint2 := intSecondsFromDuration(*(*source).Timeout) + pInt2 = &xint2 + } + schemaLoadBalancerActionUpdateServiceRequestHealthCheck.Timeout = pInt2 + schemaLoadBalancerActionUpdateServiceRequestHealthCheck.Retries = (*source).Retries + schemaLoadBalancerActionUpdateServiceRequestHealthCheck.HTTP = c.pHcloudLoadBalancerUpdateServiceOptsHealthCheckHTTPToPSchemaLoadBalancerActionUpdateServiceRequestHealthCheckHTTP((*source).HTTP) + pSchemaLoadBalancerActionUpdateServiceRequestHealthCheck = &schemaLoadBalancerActionUpdateServiceRequestHealthCheck + } + return pSchemaLoadBalancerActionUpdateServiceRequestHealthCheck +} +func (c *converterImpl) pHcloudLocationToPString(source *Location) *string { + var pString *string + if source != nil { + xstring := stringFromLocation((*source)) + pString = &xstring + } + return pString +} +func (c *converterImpl) pHcloudLocationToString(source *Location) string { + var xstring string + if source != nil { + xstring = stringFromLocation((*source)) + } + return xstring +} +func (c *converterImpl) pHcloudNetworkToInt64(source *Network) int64 { + var xint64 int64 + if source != nil { + xint64 = int64FromNetwork((*source)) + } + return xint64 +} +func (c *converterImpl) pHcloudNetworkToPInt64(source *Network) *int64 { + var pInt64 *int64 + if source != nil { + xint64 := int64FromNetwork((*source)) + pInt64 = &xint64 + } + return pInt64 +} +func (c *converterImpl) pHcloudPlacementGroupToPSchemaPlacementGroup(source *PlacementGroup) *schema.PlacementGroup { + var pSchemaPlacementGroup *schema.PlacementGroup + if source != nil { + var schemaPlacementGroup schema.PlacementGroup + schemaPlacementGroup.ID = (*source).ID + schemaPlacementGroup.Name = (*source).Name + schemaPlacementGroup.Labels = (*source).Labels + schemaPlacementGroup.Created = c.timeTimeToTimeTime((*source).Created) + schemaPlacementGroup.Servers = (*source).Servers + schemaPlacementGroup.Type = string((*source).Type) + pSchemaPlacementGroup = &schemaPlacementGroup + } + return pSchemaPlacementGroup +} +func (c *converterImpl) pHcloudServerToInt64(source *Server) int64 { + var xint64 int64 + if source != nil { + xint64 = int64FromServer((*source)) + } + return xint64 +} +func (c *converterImpl) pHcloudServerToPInt64(source *Server) *int64 { + var pInt64 *int64 + if source != nil { + xint64 := int64FromServer((*source)) + pInt64 = &xint64 + } + return pInt64 +} +func (c *converterImpl) pHcloudServerToPSchemaImageCreatedFrom(source *Server) *schema.ImageCreatedFrom { + var pSchemaImageCreatedFrom *schema.ImageCreatedFrom + if source != nil { + var schemaImageCreatedFrom schema.ImageCreatedFrom + schemaImageCreatedFrom.ID = (*source).ID + schemaImageCreatedFrom.Name = (*source).Name + pSchemaImageCreatedFrom = &schemaImageCreatedFrom + } + return pSchemaImageCreatedFrom +} +func (c *converterImpl) pNetIPNetToString(source *net.IPNet) string { + var xstring string + if source != nil { + xstring = stringFromIPNet((*source)) + } + return xstring +} +func (c *converterImpl) pSchemaCertificateStatusRefToPHcloudCertificateStatus(source *schema.CertificateStatusRef) *CertificateStatus { + var pHcloudCertificateStatus *CertificateStatus + if source != nil { + var hcloudCertificateStatus CertificateStatus + hcloudCertificateStatus.Issuance = CertificateStatusType((*source).Issuance) + hcloudCertificateStatus.Renewal = CertificateStatusType((*source).Renewal) + hcloudCertificateStatus.Error = c.pSchemaErrorToPHcloudError((*source).Error) + pHcloudCertificateStatus = &hcloudCertificateStatus + } + return pHcloudCertificateStatus +} +func (c *converterImpl) pSchemaErrorToPHcloudError(source *schema.Error) *Error { + var pHcloudError *Error + if source != nil { + hcloudError := c.ErrorFromSchema((*source)) + pHcloudError = &hcloudError + } + return pHcloudError +} +func (c *converterImpl) pSchemaFirewallResourceLabelSelectorToPHcloudFirewallResourceLabelSelector(source *schema.FirewallResourceLabelSelector) *FirewallResourceLabelSelector { + var pHcloudFirewallResourceLabelSelector *FirewallResourceLabelSelector + if source != nil { + var hcloudFirewallResourceLabelSelector FirewallResourceLabelSelector + hcloudFirewallResourceLabelSelector.Selector = (*source).Selector + pHcloudFirewallResourceLabelSelector = &hcloudFirewallResourceLabelSelector + } + return pHcloudFirewallResourceLabelSelector +} +func (c *converterImpl) pSchemaFirewallResourceServerToPHcloudFirewallResourceServer(source *schema.FirewallResourceServer) *FirewallResourceServer { + var pHcloudFirewallResourceServer *FirewallResourceServer + if source != nil { + var hcloudFirewallResourceServer FirewallResourceServer + hcloudFirewallResourceServer.ID = (*source).ID + pHcloudFirewallResourceServer = &hcloudFirewallResourceServer + } + return pHcloudFirewallResourceServer +} +func (c *converterImpl) pSchemaISOToPHcloudISO(source *schema.ISO) *ISO { + var pHcloudISO *ISO + if source != nil { + var hcloudISO ISO + hcloudISO.ID = (*source).ID + hcloudISO.Name = (*source).Name + hcloudISO.Description = (*source).Description + hcloudISO.Type = ISOType((*source).Type) + var pHcloudArchitecture *Architecture + if (*source).Architecture != nil { + hcloudArchitecture := Architecture(*(*source).Architecture) + pHcloudArchitecture = &hcloudArchitecture + } + hcloudISO.Architecture = pHcloudArchitecture + hcloudISO.Deprecated = c.timeTimeToTimeTime((*source).Deprecated) + hcloudISO.DeprecatableResource = c.schemaDeprecatableResourceToHcloudDeprecatableResource((*source).DeprecatableResource) + pHcloudISO = &hcloudISO + } + return pHcloudISO +} +func (c *converterImpl) pSchemaImageCreatedFromToPHcloudServer(source *schema.ImageCreatedFrom) *Server { + var pHcloudServer *Server + if source != nil { + hcloudServer := serverFromImageCreatedFromSchema((*source)) + pHcloudServer = &hcloudServer + } + return pHcloudServer +} +func (c *converterImpl) pSchemaImageToPHcloudImage(source *schema.Image) *Image { + var pHcloudImage *Image + if source != nil { + var hcloudImage Image + hcloudImage.ID = (*source).ID + var xstring string + if (*source).Name != nil { + xstring = *(*source).Name + } + hcloudImage.Name = xstring + hcloudImage.Type = ImageType((*source).Type) + hcloudImage.Status = ImageStatus((*source).Status) + hcloudImage.Description = (*source).Description + var xfloat32 float32 + if (*source).ImageSize != nil { + xfloat32 = *(*source).ImageSize + } + hcloudImage.ImageSize = xfloat32 + hcloudImage.DiskSize = (*source).DiskSize + hcloudImage.Created = c.timeTimeToTimeTime((*source).Created) + hcloudImage.CreatedFrom = c.pSchemaImageCreatedFromToPHcloudServer((*source).CreatedFrom) + var pHcloudServer *Server + if (*source).BoundTo != nil { + hcloudServer := serverFromInt64(*(*source).BoundTo) + pHcloudServer = &hcloudServer + } + hcloudImage.BoundTo = pHcloudServer + hcloudImage.RapidDeploy = (*source).RapidDeploy + hcloudImage.OSFlavor = (*source).OSFlavor + var xstring2 string + if (*source).OSVersion != nil { + xstring2 = *(*source).OSVersion + } + hcloudImage.OSVersion = xstring2 + hcloudImage.Architecture = Architecture((*source).Architecture) + hcloudImage.Protection = c.schemaImageProtectionToHcloudImageProtection((*source).Protection) + hcloudImage.Deprecated = c.timeTimeToTimeTime((*source).Deprecated) + hcloudImage.Labels = (*source).Labels + hcloudImage.Deleted = c.timeTimeToTimeTime((*source).Deleted) + pHcloudImage = &hcloudImage + } + return pHcloudImage +} +func (c *converterImpl) pSchemaLoadBalancerServiceHTTPToHcloudLoadBalancerServiceHTTP(source *schema.LoadBalancerServiceHTTP) LoadBalancerServiceHTTP { + var hcloudLoadBalancerServiceHTTP LoadBalancerServiceHTTP + if source != nil { + var hcloudLoadBalancerServiceHTTP2 LoadBalancerServiceHTTP + hcloudLoadBalancerServiceHTTP2.CookieName = (*source).CookieName + hcloudLoadBalancerServiceHTTP2.CookieLifetime = durationFromIntSeconds((*source).CookieLifetime) + var pHcloudCertificateList []*Certificate + if (*source).Certificates != nil { + pHcloudCertificateList = make([]*Certificate, len((*source).Certificates)) + for i := 0; i < len((*source).Certificates); i++ { + pHcloudCertificateList[i] = certificateFromInt64((*source).Certificates[i]) + } + } + hcloudLoadBalancerServiceHTTP2.Certificates = pHcloudCertificateList + hcloudLoadBalancerServiceHTTP2.RedirectHTTP = (*source).RedirectHTTP + hcloudLoadBalancerServiceHTTP2.StickySessions = (*source).StickySessions + hcloudLoadBalancerServiceHTTP = hcloudLoadBalancerServiceHTTP2 + } + return hcloudLoadBalancerServiceHTTP +} +func (c *converterImpl) pSchemaLoadBalancerServiceHealthCheckHTTPToPHcloudLoadBalancerServiceHealthCheckHTTP(source *schema.LoadBalancerServiceHealthCheckHTTP) *LoadBalancerServiceHealthCheckHTTP { + var pHcloudLoadBalancerServiceHealthCheckHTTP *LoadBalancerServiceHealthCheckHTTP + if source != nil { + var hcloudLoadBalancerServiceHealthCheckHTTP LoadBalancerServiceHealthCheckHTTP + hcloudLoadBalancerServiceHealthCheckHTTP.Domain = (*source).Domain + hcloudLoadBalancerServiceHealthCheckHTTP.Path = (*source).Path + hcloudLoadBalancerServiceHealthCheckHTTP.Response = (*source).Response + hcloudLoadBalancerServiceHealthCheckHTTP.StatusCodes = (*source).StatusCodes + hcloudLoadBalancerServiceHealthCheckHTTP.TLS = (*source).TLS + pHcloudLoadBalancerServiceHealthCheckHTTP = &hcloudLoadBalancerServiceHealthCheckHTTP + } + return pHcloudLoadBalancerServiceHealthCheckHTTP +} +func (c *converterImpl) pSchemaLoadBalancerTargetIPToPHcloudLoadBalancerTargetIP(source *schema.LoadBalancerTargetIP) *LoadBalancerTargetIP { + var pHcloudLoadBalancerTargetIP *LoadBalancerTargetIP + if source != nil { + var hcloudLoadBalancerTargetIP LoadBalancerTargetIP + hcloudLoadBalancerTargetIP.IP = (*source).IP + pHcloudLoadBalancerTargetIP = &hcloudLoadBalancerTargetIP + } + return pHcloudLoadBalancerTargetIP +} +func (c *converterImpl) pSchemaLoadBalancerTargetLabelSelectorToPHcloudLoadBalancerTargetLabelSelector(source *schema.LoadBalancerTargetLabelSelector) *LoadBalancerTargetLabelSelector { + var pHcloudLoadBalancerTargetLabelSelector *LoadBalancerTargetLabelSelector + if source != nil { + var hcloudLoadBalancerTargetLabelSelector LoadBalancerTargetLabelSelector + hcloudLoadBalancerTargetLabelSelector.Selector = (*source).Selector + pHcloudLoadBalancerTargetLabelSelector = &hcloudLoadBalancerTargetLabelSelector + } + return pHcloudLoadBalancerTargetLabelSelector +} +func (c *converterImpl) pSchemaLoadBalancerTargetServerToPHcloudLoadBalancerTargetServer(source *schema.LoadBalancerTargetServer) *LoadBalancerTargetServer { + var pHcloudLoadBalancerTargetServer *LoadBalancerTargetServer + if source != nil { + hcloudLoadBalancerTargetServer := c.LoadBalancerTargetServerFromSchema((*source)) + pHcloudLoadBalancerTargetServer = &hcloudLoadBalancerTargetServer + } + return pHcloudLoadBalancerTargetServer +} +func (c *converterImpl) pSchemaPlacementGroupToPHcloudPlacementGroup(source *schema.PlacementGroup) *PlacementGroup { + var pHcloudPlacementGroup *PlacementGroup + if source != nil { + var hcloudPlacementGroup PlacementGroup + hcloudPlacementGroup.ID = (*source).ID + hcloudPlacementGroup.Name = (*source).Name + hcloudPlacementGroup.Labels = (*source).Labels + hcloudPlacementGroup.Created = c.timeTimeToTimeTime((*source).Created) + hcloudPlacementGroup.Servers = (*source).Servers + hcloudPlacementGroup.Type = PlacementGroupType((*source).Type) + pHcloudPlacementGroup = &hcloudPlacementGroup + } + return pHcloudPlacementGroup +} +func (c *converterImpl) pTimeTimeToTimeTime(source *time.Time) time.Time { + var timeTime time.Time + if source != nil { + timeTime = (*source) + } + return timeTime +} +func (c *converterImpl) schemaActionResourceReferenceToPHcloudActionResource(source schema.ActionResourceReference) *ActionResource { + var hcloudActionResource ActionResource + hcloudActionResource.ID = source.ID + hcloudActionResource.Type = ActionResourceType(source.Type) + return &hcloudActionResource +} +func (c *converterImpl) schemaCertificateUsedByRefToHcloudCertificateUsedByRef(source schema.CertificateUsedByRef) CertificateUsedByRef { + var hcloudCertificateUsedByRef CertificateUsedByRef + hcloudCertificateUsedByRef.ID = source.ID + hcloudCertificateUsedByRef.Type = CertificateUsedByRefType(source.Type) + return hcloudCertificateUsedByRef +} +func (c *converterImpl) schemaDeprecatableResourceToHcloudDeprecatableResource(source schema.DeprecatableResource) DeprecatableResource { + var hcloudDeprecatableResource DeprecatableResource + hcloudDeprecatableResource.Deprecation = c.DeprecationFromSchema(source.Deprecation) + return hcloudDeprecatableResource +} +func (c *converterImpl) schemaFirewallResourceToHcloudFirewallResource(source schema.FirewallResource) FirewallResource { + var hcloudFirewallResource FirewallResource + hcloudFirewallResource.Type = FirewallResourceType(source.Type) + hcloudFirewallResource.Server = c.pSchemaFirewallResourceServerToPHcloudFirewallResourceServer(source.Server) + hcloudFirewallResource.LabelSelector = c.pSchemaFirewallResourceLabelSelectorToPHcloudFirewallResourceLabelSelector(source.LabelSelector) + return hcloudFirewallResource +} +func (c *converterImpl) schemaFirewallRuleToHcloudFirewallRule(source schema.FirewallRule) FirewallRule { + var hcloudFirewallRule FirewallRule + hcloudFirewallRule.Direction = FirewallRuleDirection(source.Direction) + var netIPNetList []net.IPNet + if source.SourceIPs != nil { + netIPNetList = make([]net.IPNet, len(source.SourceIPs)) + for i := 0; i < len(source.SourceIPs); i++ { + netIPNetList[i] = ipNetFromString(source.SourceIPs[i]) + } + } + hcloudFirewallRule.SourceIPs = netIPNetList + var netIPNetList2 []net.IPNet + if source.DestinationIPs != nil { + netIPNetList2 = make([]net.IPNet, len(source.DestinationIPs)) + for j := 0; j < len(source.DestinationIPs); j++ { + netIPNetList2[j] = ipNetFromString(source.DestinationIPs[j]) + } + } + hcloudFirewallRule.DestinationIPs = netIPNetList2 + hcloudFirewallRule.Protocol = FirewallRuleProtocol(source.Protocol) + hcloudFirewallRule.Port = source.Port + hcloudFirewallRule.Description = source.Description + return hcloudFirewallRule +} +func (c *converterImpl) schemaFloatingIPProtectionToHcloudFloatingIPProtection(source schema.FloatingIPProtection) FloatingIPProtection { + var hcloudFloatingIPProtection FloatingIPProtection + hcloudFloatingIPProtection.Delete = source.Delete + return hcloudFloatingIPProtection +} +func (c *converterImpl) schemaFromFloatingIPPricing(source FloatingIPPricing) schema.PricingFloatingIP { + var schemaPricingFloatingIP schema.PricingFloatingIP + schemaPricingFloatingIP.PriceMonthly = c.hcloudPriceToSchemaPrice(source.Monthly) + return schemaPricingFloatingIP +} +func (c *converterImpl) schemaFromFloatingIPTypeLocationPricing(source FloatingIPTypeLocationPricing) schema.PricingFloatingIPTypePrice { + var schemaPricingFloatingIPTypePrice schema.PricingFloatingIPTypePrice + schemaPricingFloatingIPTypePrice.Location = c.pHcloudLocationToString(source.Location) + schemaPricingFloatingIPTypePrice.PriceMonthly = c.hcloudPriceToSchemaPrice(source.Monthly) + return schemaPricingFloatingIPTypePrice +} +func (c *converterImpl) schemaFromFloatingIPTypePricing(source FloatingIPTypePricing) schema.PricingFloatingIPType { + var schemaPricingFloatingIPType schema.PricingFloatingIPType + schemaPricingFloatingIPType.Type = string(source.Type) + var schemaPricingFloatingIPTypePriceList []schema.PricingFloatingIPTypePrice + if source.Pricings != nil { + schemaPricingFloatingIPTypePriceList = make([]schema.PricingFloatingIPTypePrice, len(source.Pricings)) + for i := 0; i < len(source.Pricings); i++ { + schemaPricingFloatingIPTypePriceList[i] = c.schemaFromFloatingIPTypeLocationPricing(source.Pricings[i]) + } + } + schemaPricingFloatingIPType.Prices = schemaPricingFloatingIPTypePriceList + return schemaPricingFloatingIPType +} +func (c *converterImpl) schemaFromImagePricing(source ImagePricing) schema.PricingImage { + var schemaPricingImage schema.PricingImage + schemaPricingImage.PricePerGBMonth = c.hcloudPriceToSchemaPrice(source.PerGBMonth) + return schemaPricingImage +} +func (c *converterImpl) schemaFromLoadBalancerTypePricing(source LoadBalancerTypePricing) schema.PricingLoadBalancerType { + var schemaPricingLoadBalancerType schema.PricingLoadBalancerType + var pInt64 *int64 + if source.LoadBalancerType != nil { + pInt64 = &source.LoadBalancerType.ID + } + var xint64 int64 + if pInt64 != nil { + xint64 = *pInt64 + } + schemaPricingLoadBalancerType.ID = xint64 + var pString *string + if source.LoadBalancerType != nil { + pString = &source.LoadBalancerType.Name + } + var xstring string + if pString != nil { + xstring = *pString + } + schemaPricingLoadBalancerType.Name = xstring + var schemaPricingLoadBalancerTypePriceList []schema.PricingLoadBalancerTypePrice + if source.Pricings != nil { + schemaPricingLoadBalancerTypePriceList = make([]schema.PricingLoadBalancerTypePrice, len(source.Pricings)) + for i := 0; i < len(source.Pricings); i++ { + schemaPricingLoadBalancerTypePriceList[i] = c.SchemaFromLoadBalancerTypeLocationPricing(source.Pricings[i]) + } + } + schemaPricingLoadBalancerType.Prices = schemaPricingLoadBalancerTypePriceList + return schemaPricingLoadBalancerType +} +func (c *converterImpl) schemaFromPrimaryIPPricing(source PrimaryIPPricing) schema.PricingPrimaryIP { + var schemaPricingPrimaryIP schema.PricingPrimaryIP + schemaPricingPrimaryIP.Type = source.Type + var schemaPricingPrimaryIPTypePriceList []schema.PricingPrimaryIPTypePrice + if source.Pricings != nil { + schemaPricingPrimaryIPTypePriceList = make([]schema.PricingPrimaryIPTypePrice, len(source.Pricings)) + for i := 0; i < len(source.Pricings); i++ { + schemaPricingPrimaryIPTypePriceList[i] = c.schemaFromPrimaryIPTypePricing(source.Pricings[i]) + } + } + schemaPricingPrimaryIP.Prices = schemaPricingPrimaryIPTypePriceList + return schemaPricingPrimaryIP +} +func (c *converterImpl) schemaFromPrimaryIPTypePricing(source PrimaryIPTypePricing) schema.PricingPrimaryIPTypePrice { + var schemaPricingPrimaryIPTypePrice schema.PricingPrimaryIPTypePrice + schemaPricingPrimaryIPTypePrice.Datacenter = source.Datacenter + schemaPricingPrimaryIPTypePrice.Location = source.Location + schemaPricingPrimaryIPTypePrice.PriceHourly = c.hcloudPrimaryIPPriceToSchemaPrice(source.Hourly) + schemaPricingPrimaryIPTypePrice.PriceMonthly = c.hcloudPrimaryIPPriceToSchemaPrice(source.Monthly) + return schemaPricingPrimaryIPTypePrice +} +func (c *converterImpl) schemaFromServerTypeLocationPricing(source ServerTypeLocationPricing) schema.PricingServerTypePrice { + var schemaPricingServerTypePrice schema.PricingServerTypePrice + schemaPricingServerTypePrice.Location = c.pHcloudLocationToString(source.Location) + schemaPricingServerTypePrice.PriceHourly = c.hcloudPriceToSchemaPrice(source.Hourly) + schemaPricingServerTypePrice.PriceMonthly = c.hcloudPriceToSchemaPrice(source.Monthly) + return schemaPricingServerTypePrice +} +func (c *converterImpl) schemaFromServerTypePricing(source ServerTypePricing) schema.PricingServerType { + var schemaPricingServerType schema.PricingServerType + var pInt64 *int64 + if source.ServerType != nil { + pInt64 = &source.ServerType.ID + } + var xint64 int64 + if pInt64 != nil { + xint64 = *pInt64 + } + schemaPricingServerType.ID = xint64 + var pString *string + if source.ServerType != nil { + pString = &source.ServerType.Name + } + var xstring string + if pString != nil { + xstring = *pString + } + schemaPricingServerType.Name = xstring + var schemaPricingServerTypePriceList []schema.PricingServerTypePrice + if source.Pricings != nil { + schemaPricingServerTypePriceList = make([]schema.PricingServerTypePrice, len(source.Pricings)) + for i := 0; i < len(source.Pricings); i++ { + schemaPricingServerTypePriceList[i] = c.schemaFromServerTypeLocationPricing(source.Pricings[i]) + } + } + schemaPricingServerType.Prices = schemaPricingServerTypePriceList + return schemaPricingServerType +} +func (c *converterImpl) schemaFromTrafficPricing(source TrafficPricing) schema.PricingTraffic { + var schemaPricingTraffic schema.PricingTraffic + schemaPricingTraffic.PricePerTB = c.hcloudPriceToSchemaPrice(source.PerTB) + return schemaPricingTraffic +} +func (c *converterImpl) schemaFromVolumePricing(source VolumePricing) schema.PricingVolume { + var schemaPricingVolume schema.PricingVolume + schemaPricingVolume.PricePerGBPerMonth = c.hcloudPriceToSchemaPrice(source.PerGBMonthly) + return schemaPricingVolume +} +func (c *converterImpl) schemaImageProtectionToHcloudImageProtection(source schema.ImageProtection) ImageProtection { + var hcloudImageProtection ImageProtection + hcloudImageProtection.Delete = source.Delete + return hcloudImageProtection +} +func (c *converterImpl) schemaLoadBalancerAlgorithmToHcloudLoadBalancerAlgorithm(source schema.LoadBalancerAlgorithm) LoadBalancerAlgorithm { + var hcloudLoadBalancerAlgorithm LoadBalancerAlgorithm + hcloudLoadBalancerAlgorithm.Type = LoadBalancerAlgorithmType(source.Type) + return hcloudLoadBalancerAlgorithm +} +func (c *converterImpl) schemaLoadBalancerPrivateNetToHcloudLoadBalancerPrivateNet(source schema.LoadBalancerPrivateNet) LoadBalancerPrivateNet { + var hcloudLoadBalancerPrivateNet LoadBalancerPrivateNet + hcloudNetwork := networkFromInt64(source.Network) + hcloudLoadBalancerPrivateNet.Network = &hcloudNetwork + hcloudLoadBalancerPrivateNet.IP = ipFromString(source.IP) + return hcloudLoadBalancerPrivateNet +} +func (c *converterImpl) schemaLoadBalancerProtectionToHcloudLoadBalancerProtection(source schema.LoadBalancerProtection) LoadBalancerProtection { + var hcloudLoadBalancerProtection LoadBalancerProtection + hcloudLoadBalancerProtection.Delete = source.Delete + return hcloudLoadBalancerProtection +} +func (c *converterImpl) schemaLoadBalancerPublicNetIPv4ToHcloudLoadBalancerPublicNetIPv4(source schema.LoadBalancerPublicNetIPv4) LoadBalancerPublicNetIPv4 { + var hcloudLoadBalancerPublicNetIPv4 LoadBalancerPublicNetIPv4 + hcloudLoadBalancerPublicNetIPv4.IP = ipFromString(source.IP) + hcloudLoadBalancerPublicNetIPv4.DNSPtr = source.DNSPtr + return hcloudLoadBalancerPublicNetIPv4 +} +func (c *converterImpl) schemaLoadBalancerPublicNetIPv6ToHcloudLoadBalancerPublicNetIPv6(source schema.LoadBalancerPublicNetIPv6) LoadBalancerPublicNetIPv6 { + var hcloudLoadBalancerPublicNetIPv6 LoadBalancerPublicNetIPv6 + hcloudLoadBalancerPublicNetIPv6.IP = ipFromString(source.IP) + hcloudLoadBalancerPublicNetIPv6.DNSPtr = source.DNSPtr + return hcloudLoadBalancerPublicNetIPv6 +} +func (c *converterImpl) schemaLoadBalancerPublicNetToHcloudLoadBalancerPublicNet(source schema.LoadBalancerPublicNet) LoadBalancerPublicNet { + var hcloudLoadBalancerPublicNet LoadBalancerPublicNet + hcloudLoadBalancerPublicNet.Enabled = source.Enabled + hcloudLoadBalancerPublicNet.IPv4 = c.schemaLoadBalancerPublicNetIPv4ToHcloudLoadBalancerPublicNetIPv4(source.IPv4) + hcloudLoadBalancerPublicNet.IPv6 = c.schemaLoadBalancerPublicNetIPv6ToHcloudLoadBalancerPublicNetIPv6(source.IPv6) + return hcloudLoadBalancerPublicNet +} +func (c *converterImpl) schemaNetworkProtectionToHcloudNetworkProtection(source schema.NetworkProtection) NetworkProtection { + var hcloudNetworkProtection NetworkProtection + hcloudNetworkProtection.Delete = source.Delete + return hcloudNetworkProtection +} +func (c *converterImpl) schemaPricingServerBackupToHcloudServerBackupPricing(source schema.PricingServerBackup) ServerBackupPricing { + var hcloudServerBackupPricing ServerBackupPricing + hcloudServerBackupPricing.Percentage = source.Percentage + return hcloudServerBackupPricing +} +func (c *converterImpl) schemaPrimaryIPProtectionToHcloudPrimaryIPProtection(source schema.PrimaryIPProtection) PrimaryIPProtection { + var hcloudPrimaryIPProtection PrimaryIPProtection + hcloudPrimaryIPProtection.Delete = source.Delete + return hcloudPrimaryIPProtection +} +func (c *converterImpl) schemaServerProtectionToHcloudServerProtection(source schema.ServerProtection) ServerProtection { + var hcloudServerProtection ServerProtection + hcloudServerProtection.Delete = source.Delete + hcloudServerProtection.Rebuild = source.Rebuild + return hcloudServerProtection +} +func (c *converterImpl) schemaVolumeProtectionToHcloudVolumeProtection(source schema.VolumeProtection) VolumeProtection { + var hcloudVolumeProtection VolumeProtection + hcloudVolumeProtection.Delete = source.Delete + return hcloudVolumeProtection +} +func (c *converterImpl) serverTypePricingFromSchema(source schema.PricingServerTypePrice) ServerTypeLocationPricing { + var hcloudServerTypeLocationPricing ServerTypeLocationPricing + hcloudLocation := locationFromString(source.Location) + hcloudServerTypeLocationPricing.Location = &hcloudLocation + hcloudServerTypeLocationPricing.Hourly = c.PriceFromSchema(source.PriceHourly) + hcloudServerTypeLocationPricing.Monthly = c.PriceFromSchema(source.PriceMonthly) + return hcloudServerTypeLocationPricing +} +func (c *converterImpl) timeTimeToTimeTime(source time.Time) time.Time { + return source +} +func (c *converterImpl) unnamedToHcloudDatacenterServerTypes(source struct { + Supported []int64 `json:"supported"` + Available []int64 `json:"available"` +}) DatacenterServerTypes { + var hcloudDatacenterServerTypes DatacenterServerTypes + var pHcloudServerTypeList []*ServerType + if source.Supported != nil { + pHcloudServerTypeList = make([]*ServerType, len(source.Supported)) + for i := 0; i < len(source.Supported); i++ { + pHcloudServerTypeList[i] = serverTypeFromInt64(source.Supported[i]) + } + } + hcloudDatacenterServerTypes.Supported = pHcloudServerTypeList + var pHcloudServerTypeList2 []*ServerType + if source.Available != nil { + pHcloudServerTypeList2 = make([]*ServerType, len(source.Available)) + for j := 0; j < len(source.Available); j++ { + pHcloudServerTypeList2[j] = serverTypeFromInt64(source.Available[j]) + } + } + hcloudDatacenterServerTypes.Available = pHcloudServerTypeList2 + return hcloudDatacenterServerTypes +} diff --git a/script/generate_schema.sh b/script/generate_schema.sh new file mode 100644 index 000000000..9d35c3051 --- /dev/null +++ b/script/generate_schema.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +set -u + +sed="sed -i \'\'" +if sed --version >/dev/null 2>&1; then # BSD sed exits with 1 on `sed --version`, GNU sed doesn't + sed="sed -i" # GNU sed +fi + +# Delete zz_schema.go if it exists so that there are no compiler errors during generation. +rm -f zz_schema.go + +# Replace "var c converter = &converterImpl{}" with "var c converter" to regenerate the schema. +# This is done so that there is no compiler error during generation. +$sed -e "s/var c converter = \&converterImpl{}/var c converter/g" schema.go + +# Generate zz_schema.go +go run github.com/jmattheis/goverter/cmd/goverter gen ./... + +# After the file is generated we can add the reference back in. +$sed -e "s/var c converter/var c converter = \&converterImpl{}/g" schema.go diff --git a/tools.go b/tools.go index 9d765d5a2..4ca84ddc7 100644 --- a/tools.go +++ b/tools.go @@ -4,5 +4,6 @@ package tools import ( + _ "github.com/jmattheis/goverter/cmd/goverter" _ "github.com/vburenin/ifacemaker" )