-
Notifications
You must be signed in to change notification settings - Fork 1
/
describe_instance_types.go
80 lines (65 loc) · 2.26 KB
/
describe_instance_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Copyright 2020 Joyent, Inc.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
package actions
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"net/http"
"github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
tritoncompute "github.com/joyent/triton-go/v2/compute"
tritonutils "github.com/joyent/triton-shim/utils/triton"
)
func DescribeInstanceTypes(c *gin.Context) {
client, err := tritonutils.GetTritonComputeClient()
if err != nil {
c.AbortWithError(http.StatusInternalServerError,
fmt.Errorf("Unable to create triton compute client: %w", err))
return
}
packageListInput := &tritoncompute.ListPackagesInput{}
packages, err := client.Packages().List(context.Background(), packageListInput)
if err != nil {
log.Printf("[ERROR] list packages error: %v\n", err)
c.AbortWithError(http.StatusInternalServerError,
fmt.Errorf("Unable to list triton compute packages: %w", err))
return
}
log.Printf("[DEBUG] loaded %d packages\n", len(packages))
// Convert Triton package to AWS AMI.
ec2Output := ec2.DescribeInstanceTypesOutput{}
if len(packages) > 0 {
for _, pkg := range packages {
instType := &ec2.InstanceTypeInfo{
InstanceType: &pkg.Name,
MemoryInfo: &ec2.MemoryInfo{SizeInMiB: &pkg.Memory},
}
ec2Output.InstanceTypes = append(ec2Output.InstanceTypes, instType)
}
}
// Generate the XML response.
var buf bytes.Buffer
buf.WriteString(`<?xml version="1.0" encoding="UTF-8"?>` + "\n")
buf.WriteString(`<DescribeInstanceTypesResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">` + "\n")
// Build the XML from the AWS struct.
err = xmlutil.BuildXML(ec2Output, xml.NewEncoder(&buf))
if err != nil {
log.Printf("[ERROR] xmlutil.BuildXML error: %v\n", err)
c.AbortWithError(http.StatusInternalServerError,
fmt.Errorf("Unable to translate triton compute instances: %w", err))
return
}
buf.WriteString(`</DescribeInstanceTypesResponse>` + "\n")
// Send the XML response.
c.Header("content-type", "text/xml;charset=UTF-8")
c.Data(http.StatusOK, "", buf.Bytes())
}