|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 9 | + sca "github.com/stackitcloud/stackit-sdk-go/services/sca/v1alphaapi" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/services/sca/v1alphaapi/wait" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + region := "eu01" // Region where the resources will be created |
| 15 | + projectID := "PROJECT_ID" // the uuid of your STACKIT project |
| 16 | + |
| 17 | + // Create a new API client, that uses default authentication and configuration |
| 18 | + scaClient, err := sca.NewAPIClient(config.WithRegion(region)) |
| 19 | + if err != nil { |
| 20 | + fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err) |
| 21 | + os.Exit(1) |
| 22 | + } |
| 23 | + |
| 24 | + // Create environment |
| 25 | + // WARNING: Keep in mind that there is no endpoint to delete environments right now. |
| 26 | + createEnvironmentPayload := sca.CreateEnvironmentPayload{ |
| 27 | + DisplayName: "environment-name", |
| 28 | + } |
| 29 | + env, err := scaClient.DefaultAPI.CreateEnvironment(context.Background(), projectID). |
| 30 | + CreateEnvironmentPayload(createEnvironmentPayload).Execute() |
| 31 | + if err != nil { |
| 32 | + fmt.Fprintf(os.Stderr, "Error when calling `CreateEnvironment`: %v\n", err) |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | + |
| 36 | + fmt.Printf("Created environment with id %q\n", env.GetId()) |
| 37 | + |
| 38 | + // Get environment |
| 39 | + getEnvResp, err := scaClient.DefaultAPI.GetEnvironment(context.Background(), projectID, env.GetId()).Execute() |
| 40 | + if err != nil { |
| 41 | + fmt.Fprintf(os.Stderr, "Error when calling `GetEnvironment`: %v\n", err) |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + fmt.Printf("Got environment with id %q\n", getEnvResp.GetId()) |
| 46 | + |
| 47 | + // List environments |
| 48 | + listEnvsResp, err := scaClient.DefaultAPI.ListEnvironments(context.Background(), projectID).Execute() |
| 49 | + if err != nil { |
| 50 | + fmt.Fprintf(os.Stderr, "Error when calling `ListEnvironments`: %v\n", err) |
| 51 | + os.Exit(1) |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Printf("Number of environments in project: %d\n", len(listEnvsResp.Items)) |
| 55 | + |
| 56 | + // List all applications of a given project |
| 57 | + listAppsResp, err := scaClient.DefaultAPI.ListProjectApplications(context.Background(), projectID).Execute() |
| 58 | + if err != nil { |
| 59 | + fmt.Fprintf(os.Stderr, "Error when calling `ListProjectApplications`: %v\n", err) |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | + |
| 63 | + fmt.Printf("Number of applications in project: %d\n", len(listAppsResp.Items)) |
| 64 | + |
| 65 | + // Create an application within an environment |
| 66 | + createApplicationPayload := sca.CreateApplicationPayload{ |
| 67 | + DisplayName: "application-name", |
| 68 | + Containers: []sca.Container{{ |
| 69 | + Name: "nginx-container", |
| 70 | + Image: "nginxinc/nginx-unprivileged", |
| 71 | + Memory: sca.PtrInt32(1000), |
| 72 | + Cpu: sca.PtrInt32(1000), |
| 73 | + }}, |
| 74 | + Network: sca.Network{ |
| 75 | + PublicIngress: true, |
| 76 | + Port: sca.PtrInt32(8080), |
| 77 | + }, |
| 78 | + Scaling: sca.Scaling{ |
| 79 | + Type: sca.SCALINGTYPE_SCALING_TYPE_MANUAL, |
| 80 | + ManualScaling: &sca.ManualScaling{ |
| 81 | + Instances: 1, |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + app, err := scaClient.DefaultAPI.CreateApplication(context.Background(), projectID, env.GetId()). |
| 87 | + CreateApplicationPayload(createApplicationPayload).Execute() |
| 88 | + if err != nil { |
| 89 | + fmt.Fprintf(os.Stderr, "Error when calling `CreateApplication`: %v\n", err) |
| 90 | + os.Exit(1) |
| 91 | + } |
| 92 | + |
| 93 | + fmt.Printf("Triggered application creation with id %q\n", app.GetId()) |
| 94 | + |
| 95 | + _, err = wait.CreateApplicationWaitHandler(context.Background(), scaClient.DefaultAPI, projectID, env.GetId(), app.GetId()).WaitWithContext(context.Background()) |
| 96 | + if err != nil { |
| 97 | + fmt.Fprintf(os.Stderr, "Error when calling `CreateApplicationWaitHandler`: %v\n", err) |
| 98 | + os.Exit(1) |
| 99 | + } |
| 100 | + |
| 101 | + fmt.Printf("Application created with id %q\n", app.GetId()) |
| 102 | + |
| 103 | + // Get application's details |
| 104 | + getAppResp, err := scaClient.DefaultAPI.GetApplication(context.Background(), projectID, env.GetId(), app.GetId()).Execute() |
| 105 | + if err != nil { |
| 106 | + fmt.Fprintf(os.Stderr, "Error when calling `GetApplication`: %v\n", err) |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Printf("Got application with id %q\n", getAppResp.GetId()) |
| 111 | + |
| 112 | + // List applications in an environment |
| 113 | + listEnvAppsResp, err := scaClient.DefaultAPI.ListApplications(context.Background(), projectID, env.GetId()).Execute() |
| 114 | + if err != nil { |
| 115 | + fmt.Fprintf(os.Stderr, "Error when calling `ListApplications`: %v\n", err) |
| 116 | + os.Exit(1) |
| 117 | + } |
| 118 | + |
| 119 | + fmt.Printf("Number of applications in environment: %d\n", len(listEnvAppsResp.Items)) |
| 120 | + |
| 121 | + logs, err := scaClient.DefaultAPI.GetApplicationLogs(context.Background(), projectID, env.GetId(), app.GetId()). |
| 122 | + Instance(getAppResp.RuntimeStatus.Instances[0].GetName()). |
| 123 | + Container(getAppResp.Containers[0].GetName()). |
| 124 | + Execute() |
| 125 | + if err != nil { |
| 126 | + fmt.Fprintf(os.Stderr, "Error when calling `GetApplicationLogs`: %v\n", err) |
| 127 | + os.Exit(1) |
| 128 | + } |
| 129 | + |
| 130 | + fmt.Printf("Found %d logs for application %q\n", len(logs.GetLogs()), app.GetId()) |
| 131 | + |
| 132 | + events, err := scaClient.DefaultAPI.GetApplicationEvents(context.Background(), projectID, env.GetId(), app.GetId()).Execute() |
| 133 | + if err != nil { |
| 134 | + fmt.Fprintf(os.Stderr, "Error when calling `GetApplicationEvents`: %v\n", err) |
| 135 | + os.Exit(1) |
| 136 | + } |
| 137 | + |
| 138 | + fmt.Printf("Found %d events for application %q\n", len(events.GetEvents()), app.GetId()) |
| 139 | + |
| 140 | + // Update application |
| 141 | + updateApplicationPayload := sca.UpdateApplicationPayload{ |
| 142 | + Scaling: &sca.Scaling{ |
| 143 | + Type: sca.SCALINGTYPE_SCALING_TYPE_AUTO, |
| 144 | + AutoScaling: &sca.AutoScaling{ |
| 145 | + AllowScaleToZero: sca.PtrBool(true), |
| 146 | + MinInstances: 1, |
| 147 | + MaxInstances: 2, |
| 148 | + Rules: []sca.ScaleRule{{ |
| 149 | + Name: "http-scaling-rule", |
| 150 | + Type: sca.RULETYPE_RULE_TYPE_HTTP, |
| 151 | + HttpRule: &sca.HttpScaleRule{ |
| 152 | + Concurrency: sca.PtrInt32(10), |
| 153 | + Rps: sca.PtrInt32(10), |
| 154 | + }, |
| 155 | + }}, |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | + updateAppResp, err := scaClient.DefaultAPI.UpdateApplication(context.Background(), projectID, env.GetId(), app.GetId()). |
| 160 | + UpdateApplicationPayload(updateApplicationPayload).Execute() |
| 161 | + if err != nil { |
| 162 | + fmt.Fprintf(os.Stderr, "Error when calling `UpdateApplication`: %v\n", err) |
| 163 | + os.Exit(1) |
| 164 | + } |
| 165 | + |
| 166 | + fmt.Printf("Triggered application update with id %q\n", app.GetId()) |
| 167 | + |
| 168 | + _, err = wait.UpdateApplicationWaitHandler(context.Background(), scaClient.DefaultAPI, projectID, env.GetId(), app.GetId()).WaitWithContext(context.Background()) |
| 169 | + if err != nil { |
| 170 | + fmt.Fprintf(os.Stderr, "Error when calling `UpdatedApplicationWaitHandler`: %v\n", err) |
| 171 | + os.Exit(1) |
| 172 | + } |
| 173 | + |
| 174 | + fmt.Printf("Updated application with id %q\n", updateAppResp.GetId()) |
| 175 | + |
| 176 | + deleteApplicationResp, err := scaClient.DefaultAPI.DeleteApplication(context.Background(), projectID, env.GetId(), app.GetId()).Execute() |
| 177 | + if err != nil { |
| 178 | + fmt.Fprintf(os.Stderr, "Error when calling `DeleteApplication`: %v\n", err) |
| 179 | + os.Exit(1) |
| 180 | + } |
| 181 | + |
| 182 | + fmt.Printf("Triggered application deletion with id %q\n", *deleteApplicationResp.Id) |
| 183 | + |
| 184 | + _, err = wait.DeleteApplicationWaitHandler(context.Background(), scaClient.DefaultAPI, projectID, env.GetId(), app.GetId()).WaitWithContext(context.Background()) |
| 185 | + if err != nil { |
| 186 | + fmt.Fprintf(os.Stderr, "Error when calling `DeleteApplicationWaitHandler`: %v\n", err) |
| 187 | + os.Exit(1) |
| 188 | + } |
| 189 | + |
| 190 | + fmt.Printf("Application deleted with id %q\n", app.GetId()) |
| 191 | +} |
0 commit comments