github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/common/model.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for infos. 3 4 package common 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 "github.com/juju/juju/apiserver/params" 11 "github.com/juju/juju/status" 12 "github.com/juju/names" 13 ) 14 15 // ModelInfo contains information about a model. 16 type ModelInfo struct { 17 Name string `json:"name" yaml:"name"` 18 UUID string `json:"model-uuid" yaml:"model-uuid"` 19 ControllerUUID string `json:"controller-uuid" yaml:"controller-uuid"` 20 Owner string `json:"owner" yaml:"owner"` 21 ProviderType string `json:"type" yaml:"type"` 22 Life string `json:"life" yaml:"life"` 23 Status ModelStatus `json:"status" yaml:"status"` 24 Users map[string]ModelUserInfo `json:"users" yaml:"users"` 25 } 26 27 // ModelStatus contains the current status of a model. 28 type ModelStatus struct { 29 Current status.Status `json:"current" yaml:"current"` 30 Message string `json:"message,omitempty" yaml:"message,omitempty"` 31 Since string `json:"since,omitempty" yaml:"since,omitempty"` 32 } 33 34 // ModelUserInfo defines the serialization behaviour of the model user 35 // information. 36 type ModelUserInfo struct { 37 DisplayName string `yaml:"display-name,omitempty" json:"display-name,omitempty"` 38 Access string `yaml:"access" json:"access"` 39 LastConnection string `yaml:"last-connection" json:"last-connection"` 40 } 41 42 // ModelInfoFromParams translates a params.ModelInfo to ModelInfo. 43 func ModelInfoFromParams(info params.ModelInfo, now time.Time) (ModelInfo, error) { 44 tag, err := names.ParseUserTag(info.OwnerTag) 45 if err != nil { 46 return ModelInfo{}, errors.Trace(err) 47 } 48 status := ModelStatus{ 49 Current: info.Status.Status, 50 Message: info.Status.Info, 51 } 52 if info.Status.Since != nil { 53 status.Since = UserFriendlyDuration(*info.Status.Since, now) 54 } 55 return ModelInfo{ 56 Name: info.Name, 57 UUID: info.UUID, 58 ControllerUUID: info.ControllerUUID, 59 Owner: tag.Id(), 60 Life: string(info.Life), 61 Status: status, 62 ProviderType: info.ProviderType, 63 Users: ModelUserInfoFromParams(info.Users, now), 64 }, nil 65 } 66 67 // ModelUserInfoFromParams translates []params.ModelInfo to a map of 68 // user names to ModelUserInfo. 69 func ModelUserInfoFromParams(users []params.ModelUserInfo, now time.Time) map[string]ModelUserInfo { 70 output := make(map[string]ModelUserInfo) 71 for _, info := range users { 72 outInfo := ModelUserInfo{ 73 DisplayName: info.DisplayName, 74 Access: string(info.Access), 75 } 76 if info.LastConnection != nil { 77 outInfo.LastConnection = UserFriendlyDuration(*info.LastConnection, now) 78 } else { 79 outInfo.LastConnection = "never connected" 80 } 81 output[names.NewUserTag(info.UserName).Canonical()] = outInfo 82 } 83 return output 84 }