2a41fb6eb8bde8561db3832d82b7990e2454a887 — Louis Solofrizzo 11 months ago b9c2814
api, csc, sdk: Add admin user get info

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
4 files changed, 102 insertions(+), 1 deletions(-)

M api/admin.go
M csc/cmd/admin/user/get.go
M sdk/quota.go
M sdk/request.go
M api/admin.go => api/admin.go +22 -0
@@ 21,6 21,27 @@ ctx.JSON(&result)
  }
  
+ func admin_quota_get(ctx iris.Context) {
+ 	var quota DbQuotas
+ 	var result cisco.Quota
+ 
+ 	name := ctx.Params().Get("name")
+ 	DB.First(&quota, "role = ?", name)
+ 	if quota.ID == 0 {
+ 		APIError(ctx, 404, "Quota does not exist")
+ 		return
+ 	}
+ 
+ 	result = cisco.Quota{
+ 		Instances: quota.MaxInstances,
+ 		Ipv4:      quota.MaxIpv4,
+ 		Storage:   quota.MaxStorage,
+ 		Invites:   quota.MaxInvites,
+ 	}
+ 
+ 	ctx.JSON(&result)
+ }
+ 
  func admin_quota_list(ctx iris.Context) {
  	var result []cisco.Quota
  	var quotas []DbQuotas


@@ 214,6 235,7 @@ func InitAdminRoutes(app *iris.Application) {
  	app.Get("/admin/user/list", AdminAuth, admin_user_list)
  	app.Get("/admin/quota/list", AdminAuth, admin_quota_list)
+ 	app.Get("/admin/quota/{name:string}", AdminAuth, admin_quota_get)
  	app.Put("/admin/quota/{name:string}", AdminAuth, admin_quota_create)
  	app.Delete("/admin/quota/{name:string}", AdminAuth, admin_quota_delete)
  	app.Post("/admin/quota/instance/{name:string}", AdminAuth, admin_quota_instance)

M csc/cmd/admin/user/get.go => csc/cmd/admin/user/get.go +59 -1
@@ 13,13 13,71 @@ Args:  cobra.ExactArgs(1),
  	Run: func(cmd *cobra.Command, args []string) {
  		userinfo, err := cisco.GetUserInfoAdmin(args[0])
+ 		var storage uint = 0
  
  		if err != nil {
  			fmt.Printf("Error: %s\n", err.Error())
  			return
  		}
  
- 		fmt.Printf("%v", userinfo)
+ 		fmt.Printf("User Infos:\n")
+ 		fmt.Printf("    Username   : %s\n", userinfo.User.Username)
+ 		fmt.Printf("    Email      : %s\n", userinfo.User.Email)
+ 		fmt.Printf("    Role       : %s\n", userinfo.User.Role)
+ 		fmt.Printf("    Created at : %s\n", userinfo.User.Created)
+ 
+ 		quota, err := cisco.GetQuota(userinfo.User.Role)
+ 		fmt.Printf("Quotas:\n")
+ 		if err != nil {
+ 			fmt.Printf("    Error: %s\n", err)
+ 		} else {
+ 			for _, instance := range userinfo.Instances {
+ 				storage += instance.Size
+ 			}
+ 			fmt.Printf("    Instances : (%d/%d)\n", len(userinfo.Instances), quota.Instances)
+ 			fmt.Printf("    IPV4      : (%d/%d)\n", len(userinfo.Ipv4s), quota.Ipv4)
+ 			fmt.Printf("    Storage   : (%d/%d)\n", storage, quota.Storage)
+ 			fmt.Printf("    Invites   : (%d/%d)\n", len(userinfo.Invites), quota.Invites)
+ 		}
+ 
+ 		fmt.Printf("Tokens (%d):\n", len(userinfo.Tokens))
+ 		for _, token := range userinfo.Tokens {
+ 			fmt.Printf("    Token created at %s:\n", token.Created)
+ 			fmt.Printf("        Key         : %s\n", token.Key)
+ 			fmt.Printf("        Secret      : %s\n", token.Secret)
+ 			fmt.Printf("        Description : %s\n", token.Description)
+ 		}
+ 
+ 		fmt.Printf("Instances (%d):\n", len(userinfo.Instances))
+ 		for _, instance := range userinfo.Instances {
+ 			fmt.Printf("    %s:\n", instance.Uuid)
+ 			fmt.Printf("        Name         : %s\n", instance.Name)
+ 			fmt.Printf("        Size         : %d\n", instance.Size)
+ 			fmt.Printf("        OS           : %s\n", instance.OS)
+ 			fmt.Printf("        Architecture : %s\n", instance.Architecture)
+ 			fmt.Printf("        Type         : %s\n", instance.Type)
+ 			fmt.Printf("        Created      : %s\n", instance.Created)
+ 		}
+ 
+ 		fmt.Printf("SSH Keys (%d):\n", len(userinfo.Keys))
+ 		for _, key := range userinfo.Keys {
+ 			fmt.Printf("    Key added at %s:\n", key.Created)
+ 			fmt.Printf("        Key: %s\n", key.Key)
+ 		}
+ 
+ 		fmt.Printf("IPv4s (%d):\n", len(userinfo.Ipv4s))
+ 		for _, ipv4 := range userinfo.Ipv4s {
+ 			fmt.Printf("    IP created %s at:\n", ipv4.Created)
+ 			fmt.Printf("        IP       : %s\n", ipv4.IP)
+ 			fmt.Printf("        Instance : %s\n", ipv4.Instance)
+ 		}
+ 
+ 		fmt.Printf("Invites (%d):\n", len(userinfo.Invites))
+ 		for _, invite := range userinfo.Invites {
+ 			fmt.Printf("    Invite created at %s:\n", invite.Created)
+ 			fmt.Printf("        Email   : %s\n", invite.Email)
+ 			fmt.Printf("        Claimed : %t\n", invite.Claimed)
+ 		}
  	},
  }
  

M sdk/quota.go => sdk/quota.go +15 -0
@@ 12,6 12,21 @@ Invites   int    `json:"invites"`
  }
  
+ func GetQuota(name string) (Quota, error) {
+ 	var result Quota
+ 
+ 	data, err := SendRequest(AdminQuotaGetRoute, Request{
+ 		Arg: name,
+ 	})
+ 
+ 	if err != nil {
+ 		return result, err
+ 	}
+ 
+ 	err = json.Unmarshal(data.([]byte), &result)
+ 	return result, err
+ }
+ 
  func ListQuota() ([]Quota, error) {
  	var result []Quota
  

M sdk/request.go => sdk/request.go +6 -0
@@ 62,6 62,12 @@ Protected: true,
  }
  
+ var AdminQuotaGetRoute Route = Route{
+ 	Path:      "/admin/quota",
+ 	Method:    "GET",
+ 	Protected: true,
+ }
+ 
  var AdminQuotaListRoute Route = Route{
  	Path:      "/admin/quota/list",
  	Method:    "GET",