-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from kaytu-io/fix-get-recom
fix: add kaytu request
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package kaytu | ||
|
||
import ( | ||
"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb" | ||
) | ||
|
||
type GcpComputeInstance struct { | ||
HashedInstanceId string `json:"hashedInstanceId"` | ||
Zone string `json:"zone"` | ||
MachineType string `json:"machineType"` | ||
} | ||
|
||
type RightsizingGcpComputeInstance struct { | ||
Zone string `json:"zone"` | ||
MachineType string `json:"machineType"` | ||
MachineFamily string `json:"machineFamily"` | ||
CPU int64 `json:"cpu"` | ||
MemoryMb int64 `json:"memoryMb"` | ||
|
||
Cost float64 `json:"cost"` | ||
} | ||
|
||
type GcpComputeInstanceRightsizingRecommendation struct { | ||
Current RightsizingGcpComputeInstance `json:"current"` | ||
Recommended *RightsizingGcpComputeInstance `json:"recommended"` | ||
|
||
CPU Usage `json:"cpu"` | ||
Memory Usage `json:"memory"` | ||
|
||
Description string `json:"description"` | ||
} | ||
|
||
type GcpComputeInstanceWastageRequest struct { | ||
RequestId *string `json:"requestId"` | ||
CliVersion *string `json:"cliVersion"` | ||
Identification map[string]string `json:"identification"` | ||
Instance GcpComputeInstance `json:"instance"` | ||
Metrics map[string][]*monitoringpb.Point `json:"metrics"` | ||
Region string `json:"region"` | ||
Preferences map[string]*string `json:"preferences"` | ||
Loading bool `json:"loading"` | ||
} | ||
|
||
type GcpComputeInstanceWastageResponse struct { | ||
RightSizing GcpComputeInstanceRightsizingRecommendation `json:"rightSizing"` | ||
} | ||
|
||
type Usage struct { | ||
Avg *float64 | ||
Min *float64 | ||
Max *float64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package kaytu | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
var ErrLogin = errors.New("your session is expired, please login") | ||
|
||
func Ec2InstanceWastageRequest(reqBody GcpComputeInstanceWastageRequest, token string) (*GcpComputeInstanceWastageResponse, error) { | ||
payloadEncoded, err := json.Marshal(reqBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req, err := http.NewRequest("POST", "https://api.kaytu.io/kaytu/wastage/api/v1/wastage/gcp-compute", bytes.NewBuffer(payloadEncoded)) | ||
//req, err := http.NewRequest("POST", "http://localhost:8000/api/v1/wastage/ec2-instance", bytes.NewBuffer(payloadEncoded)) | ||
if err != nil { | ||
return nil, fmt.Errorf("[ec2-instance]: %v", err) | ||
} | ||
req.Header.Add("content-type", "application/json") | ||
if len(token) > 0 { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
} | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("[gcp-compute]: %v", err) | ||
} | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("[gcp-compute]: %v", err) | ||
} | ||
err = res.Body.Close() | ||
if err != nil { | ||
return nil, fmt.Errorf("[gcp-compute]: %v", err) | ||
} | ||
|
||
if res.StatusCode == 401 { | ||
return nil, ErrLogin | ||
} | ||
|
||
if res.StatusCode >= 300 || res.StatusCode < 200 { | ||
return nil, fmt.Errorf("server returned status code %d, [requestAbout] : %s", res.StatusCode, string(body)) | ||
} | ||
|
||
response := GcpComputeInstanceWastageResponse{} | ||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return nil, fmt.Errorf("[requestAbout] : %v", err) | ||
} | ||
return &response, nil | ||
} |