Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil-024 committed Sep 16, 2024
1 parent 199fe9b commit 765de27
Show file tree
Hide file tree
Showing 24 changed files with 268 additions and 6,348 deletions.
16 changes: 16 additions & 0 deletions cmd/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package describe

import (
"github.com/dream11/odin/cmd"
"github.com/spf13/cobra"
)

var describeCmd = &cobra.Command{
Use: "describe",
Short: "Describe resources",
Long: `Describe resources`,
}

func init() {
cmd.RootCmd.AddCommand(describeCmd)
}
167 changes: 167 additions & 0 deletions cmd/describe/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package describe

import (
"encoding/json"
"fmt"

"github.com/dream11/odin/internal/service"
"github.com/dream11/odin/pkg/constant"
"github.com/dream11/odin/pkg/table"
environment "github.com/dream11/odin/proto/gen/go/dream11/od/environment/v1"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var name string
var serviceName string
var component string

var environmentClient = service.Environment{}
var environmentCmd = &cobra.Command{
Use: "env",
Short: "Describe environments",
Args: func(cmd *cobra.Command, args []string) error {
return cobra.NoArgs(cmd, args)
},
Long: `Describe environment details`,
Run: func(cmd *cobra.Command, args []string) {
execute(cmd)
},
}

func init() {
environmentCmd.Flags().StringVar(&name, "name", "", "name of the env")
environmentCmd.Flags().StringVar(&serviceName, "service", "", "provisioning type of the environment")
environmentCmd.Flags().StringVar(&component, "component", "", "cloud provider account name")
describeCmd.AddCommand(environmentCmd)
}

func execute(cmd *cobra.Command) {
ctx := cmd.Context()
params := map[string]string{}

if serviceName != "" {
params["service"] = serviceName
}
if component != "" {
params["component"] = component
}
response, err := environmentClient.DescribeEnvironment(&ctx, &environment.DescribeEnvironmentRequest{
Params: params,
EnvName: name,
})

if err != nil {
log.Fatal("Failed to describe environment ", err)
}

outputFormat, err := cmd.Flags().GetString("output")
if err != nil {
log.Fatal(err)
}
writeOutput(response, outputFormat)
}

func writeOutput(response *environment.DescribeEnvironmentResponse, format string) {

switch format {
case constant.TEXT:
writeAsText(response)
case constant.JSON:
writeAsJSON(response)
default:
log.Fatal("Unknown output format: ", format)
}
}

func writeAsText(response *environment.DescribeEnvironmentResponse) {

tableHeaders := []string{"Name",
"team",
"state",
"autoDeletionTime",
"cloudProviderAccounts",
"createdBy",
"updatedBy",
"createdAt",
"updatedAt",
"services"}
var tableData [][]interface{}
env := response.Environment
var accountInfoList []string
for _, accountInfo := range env.AccountInformation {
accountInfoList = append(accountInfoList, accountInfo.ProviderAccountName)
}
accountInfoListJSON, err := json.Marshal(accountInfoList)
if err != nil {
log.Fatal("Failed to marshal account info list: ", err)
}

var servicesSummary []map[string]interface{}
for _, svc := range env.Services {
serviceMap := map[string]interface{}{
"name": svc.Name,
"version": svc.Version,
}
if len(svc.Components) > 0 {
serviceMap["components"] = svc.Components
}
servicesSummary = append(servicesSummary, serviceMap)
}
servicesSummaryJSON, err := json.Marshal(servicesSummary)
if err != nil {
log.Fatal("Failed to marshal services summary: ", err)
}

tableData = append(tableData, []interface{}{
*env.Name,
"TODO",
*env.Status,
env.AutoDeletionTime.AsTime().String(),
string(accountInfoListJSON),
*env.CreatedBy,
*env.UpdatedBy,
env.CreatedAt.AsTime().String(),
env.UpdatedAt.AsTime().String(),
string(servicesSummaryJSON),
})

table.Write(tableHeaders, tableData)
}

func writeAsJSON(response *environment.DescribeEnvironmentResponse) {
var environments []map[string]interface{}
env := response.Environment
var accountInfoList []string
for _, accountInfo := range env.AccountInformation {
accountInfoList = append(accountInfoList, accountInfo.ProviderAccountName)
}

var servicesSummary []map[string]interface{}
for _, svc := range env.Services {
serviceMap := map[string]interface{}{
"name": svc.Name,
"version": svc.Version,
}
if len(svc.Components) > 0 {
serviceMap["components"] = svc.Components
}
servicesSummary = append(servicesSummary, serviceMap)
}

environments = append(environments, map[string]interface{}{
"name": env.Name,
"team": "TODO",
"state": env.Status,
"autoDeletionTime": env.AutoDeletionTime.AsTime().String(),
"cloudProviderAccounts": accountInfoList,
"createdBy": env.CreatedBy,
"updatedBy": env.UpdatedBy,
"createdAt": env.CreatedAt.AsTime().String(),
"updatedAt": env.UpdatedAt.AsTime().String(),
"services": servicesSummary,
})

output, _ := json.MarshalIndent(environments, "", " ")
fmt.Print(string(output))
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var RootCmd = &cobra.Command{

func init() {
RootCmd.PersistentFlags().StringP("profile", "p", "default", "odin profile")
RootCmd.PersistentFlags().StringP("output", "o", "text", "odin profile")
RootCmd.PersistentFlags().StringP("output", "o", "json", "odin profile")
err := viper.BindPFlag("profile", RootCmd.PersistentFlags().Lookup("profile"))
if err != nil {
log.Fatal("Error while binding profile flag")
Expand Down
18 changes: 18 additions & 0 deletions internal/service/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (e *Environment) DeleteEnvironment(ctx *context.Context, request *environme
return err
}

// UpdateEnvironment updates environment
func (e *Environment) UpdateEnvironment(ctx *context.Context, request *environment.UpdateEnvironmentRequest) (*environment.UpdateEnvironmentResponse, error) {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
Expand All @@ -123,3 +124,20 @@ func (e *Environment) UpdateEnvironment(ctx *context.Context, request *environme

return response, nil
}

// DescribeEnvironment shows environment details including services and resources in it
func (e *Environment) DescribeEnvironment(ctx *context.Context, request *environment.DescribeEnvironmentRequest) (*environment.DescribeEnvironmentResponse, error) {
conn, requestCtx, err := grpcClient(ctx)
if err != nil {
return nil, err
}

client := environment.NewEnvironmentServiceClient(conn)
response, err := client.DescribeEnvironment(*requestCtx, request)

if err != nil {
return nil, err
}

return response, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
_ "github.com/dream11/odin/cmd/create"
_ "github.com/dream11/odin/cmd/delete"
_ "github.com/dream11/odin/cmd/deploy"
_ "github.com/dream11/odin/cmd/describe"
_ "github.com/dream11/odin/cmd/list"
_ "github.com/dream11/odin/cmd/operate"
_ "github.com/dream11/odin/cmd/set"
Expand Down
Binary file modified odin
Binary file not shown.
29 changes: 29 additions & 0 deletions proto/dream11/oam/dto/v1/provider_account.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
syntax = "proto3";
package dream11.oam.dto.v1;

import "google/protobuf/struct.proto";

option go_package = "github.com/dream11/odin/proto/gen/go/dream11/oam/dto/v1";

message ProviderAccount {
string name = 1;
string provider = 2;
string category = 3;
google.protobuf.Struct data = 4;
repeated ProviderServiceAccount services = 5;
bool default = 6;
int64 id = 7;
repeated int64 linked_provider_account_ids = 8;
}

message ProviderServiceAccountEnriched {
ProviderServiceAccount service = 1;
ProviderAccount account = 2;
}

message ProviderServiceAccount {
string name = 1;
string category = 2;
google.protobuf.Struct data = 3;
int64 id = 4;
}
31 changes: 31 additions & 0 deletions proto/dream11/oam/provideraccount/v1/provider_account.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
syntax = "proto3";
package dream11.oam.provideraccount.v1;
import "dream11/oam/dto/v1/provider_account.proto";

option go_package = "github.com/dream11/odin/proto/gen/go/dream11/oam/provideraccount/v1";

service ProviderAccountService {
rpc GetProviderAccount(GetProviderAccountRequest) returns (GetProviderAccountResponse) {}
rpc GetProviderAccounts(GetProviderAccountsRequest) returns (GetProviderAccountsResponse) {}
}

message GetProviderAccountRequest {
map<string, string> headers = 1;
string name = 2;
bool fetch_linked_account_details = 3;
}

message GetProviderAccountResponse {
dream11.oam.dto.v1.ProviderAccount account = 1;
repeated dream11.oam.dto.v1.ProviderAccount linked_accounts = 2;
}

message GetProviderAccountsRequest {
map<string, string> headers = 1;
repeated string name = 2;
bool fetch_linked_account_details = 3;
}

message GetProviderAccountsResponse {
repeated GetProviderAccountResponse accounts = 1;
}
10 changes: 4 additions & 6 deletions proto/dream11/od/dto/v1/environment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ syntax = "proto3";
package dream11.od.dto.v1;

import "dream11/od/dto/v1/service_task.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";

import "dream11/oam/provideraccount/v1/provider_account.proto";
option go_package = "github.com/dream11/odin/proto/gen/go/dream11/od/dto/v1";

message Environment {
Expand All @@ -14,18 +13,17 @@ message Environment {
optional string created_by = 4;
optional int32 version = 5;
optional int64 org_id = 6;
optional string provider_account_name = 7;
optional string name = 8;
optional google.protobuf.Struct service_accounts_snapshot = 9;
optional string status = 10;
optional google.protobuf.Timestamp auto_deletion_time = 11;
repeated dream11.od.dto.v1.ServiceTask services = 12;
repeated AccountInformation account_information = 13;
repeated AccountInformation account_information = 13;
optional string updated_by = 14;
}

message AccountInformation {
string provider_account_name = 1;
google.protobuf.Struct service_accounts_snapshot = 2;
dream11.oam.provideraccount.v1.GetProviderAccountResponse service_accounts_snapshot = 2;
string status = 3;
}

Expand Down
1 change: 1 addition & 0 deletions proto/dream11/od/environment/v1/environment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ message DeleteEnvironmentRequest {
message DeleteEnvironmentResponse {
string message = 1;
}

Loading

0 comments on commit 765de27

Please sign in to comment.