-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.go
More file actions
84 lines (68 loc) · 2.51 KB
/
Copy pathfunc.go
File metadata and controls
84 lines (68 loc) · 2.51 KB
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
81
82
83
84
package main
import (
"context"
"encoding/json"
"io"
"log"
fdk "github.com/fnproject/fdk-go"
"github.com/oracle/oci-go-sdk/v65/common"
"github.com/oracle/oci-go-sdk/v65/common/auth"
"github.com/oracle/oci-go-sdk/v65/core"
"github.com/oracle/oci-go-sdk/v65/example/helpers"
"github.com/sethvargo/go-envconfig"
)
type Config struct {
ActiveOCPU int `env:"ACTIVE_OCPU, required"`
ActiveMemory int `env:"ACTIVE_MEMORY, required"`
InActiveOCPU int `env:"INACTIVE_OCPU, required"`
InActiveMemory int `env:"INACTIVE_MEMORY, required"`
InstanceId string `env:"INSTANCE_ID, required"`
}
func main() {
fdk.Handle(fdk.HandlerFunc(resizeHandler))
}
func resizeHandler(ctx context.Context, in io.Reader, out io.Writer) {
var cfg Config
err := envconfig.Process(ctx, &cfg)
helpers.FatalIfError(err)
log.Printf("Instance ID: %s", cfg.InstanceId)
log.Printf("Active OCPU: %d", cfg.ActiveOCPU)
log.Printf("Active Memory: %d", cfg.ActiveMemory)
log.Printf("Inactive OCPU: %d", cfg.InActiveOCPU)
log.Printf("Inactive Memory: %d", cfg.InActiveMemory)
provider, err := auth.ResourcePrincipalConfigurationProvider()
helpers.FatalIfError(err)
client, err := core.NewComputeClientWithConfigurationProvider(provider)
helpers.FatalIfError(err)
// Get the instance current details
getReq := core.GetInstanceRequest{
InstanceId: common.String(cfg.InstanceId),
}
resp, err := client.GetInstance(context.Background(), getReq)
helpers.FatalIfError(err)
// Check current instance shape and set target shape
var targetOCPU, targetMemory int
if *resp.ShapeConfig.Ocpus == float32(cfg.ActiveOCPU) && *resp.ShapeConfig.MemoryInGBs == float32(cfg.ActiveMemory) {
targetOCPU = cfg.InActiveOCPU
targetMemory = cfg.InActiveMemory
} else if *resp.ShapeConfig.Ocpus == float32(cfg.InActiveOCPU) && *resp.ShapeConfig.MemoryInGBs == float32(cfg.InActiveMemory) {
targetOCPU = cfg.ActiveOCPU
targetMemory = cfg.ActiveMemory
} else {
json.NewEncoder(out).Encode("Instance shape is none of the specified shapes")
return
}
// Resize the instance
updateReq := core.UpdateInstanceRequest{
InstanceId: common.String(cfg.InstanceId),
UpdateInstanceDetails: core.UpdateInstanceDetails{
ShapeConfig: &core.UpdateInstanceShapeConfigDetails{
Ocpus: common.Float32(float32(targetOCPU)),
MemoryInGBs: common.Float32(float32(targetMemory)),
},
},
}
_, err = client.UpdateInstance(context.Background(), updateReq)
helpers.FatalIfError(err)
json.NewEncoder(out).Encode("Instance resized successfully")
}