github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/expansions/fetch_command.go (about) 1 package expansions 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/evergreen-ci/evergreen/model" 8 "github.com/evergreen-ci/evergreen/plugin" 9 "github.com/mitchellh/mapstructure" 10 "github.com/mongodb/grip" 11 "github.com/mongodb/grip/slogger" 12 "github.com/pkg/errors" 13 ) 14 15 const FetchVarsRoute = "fetch_vars" 16 const FetchVarsCmdname = "fetch" 17 18 type ExpansionVars map[string]string 19 20 // FetchVarsCommand pulls a set of vars (stored in the DB on the server side) 21 // and updates the agent's expansions map using the values it gets back 22 type FetchVarsCommand struct { 23 Keys []FetchCommandParams `mapstructure:"keys" json:"keys"` 24 } 25 26 // FetchCommandParams is a pairing of remote key and local key values 27 type FetchCommandParams struct { 28 // RemoteKey indicates which key in the projects vars map to use as the lvalue 29 RemoteKey string `mapstructure:"remote_key" json:"remote_key"` 30 31 // LocalKey indicates which key in the local expansions map to use as the rvalue 32 LocalKey string `mapstructure:"local_key" json:"local_key"` 33 } 34 35 func (self *FetchVarsCommand) Name() string { 36 return FetchVarsCmdname 37 } 38 39 func (self *FetchVarsCommand) Plugin() string { 40 return ExpansionsPluginName 41 } 42 43 // ParseParams reads in the command's config. Fulfills the Command interface. 44 func (self *FetchVarsCommand) ParseParams(params map[string]interface{}) error { 45 err := mapstructure.Decode(params, self) 46 if err != nil { 47 return err 48 } 49 50 for _, item := range self.Keys { 51 if item.RemoteKey == "" { 52 return errors.Errorf("error parsing '%v' params: value for remote "+ 53 "key must not be a blank string", self.Name()) 54 } 55 if item.LocalKey == "" { 56 return errors.Errorf("error parsing '%v' params: value for local "+ 57 "key must not be a blank string", self.Name()) 58 } 59 } 60 return nil 61 } 62 63 // FetchVarsHandler is an API hook for returning the project variables 64 // associated with a task's project. 65 func FetchVarsHandler(w http.ResponseWriter, r *http.Request) { 66 task := plugin.GetTask(r) 67 if task == nil { 68 http.Error(w, "task not found", http.StatusNotFound) 69 return 70 } 71 projectVars, err := model.FindOneProjectVars(task.Project) 72 if err != nil { 73 message := fmt.Sprintf("Failed to fetch vars for task %v: %v", task.Id, err) 74 grip.Error(message) 75 http.Error(w, message, http.StatusInternalServerError) 76 return 77 } 78 if projectVars == nil { 79 plugin.WriteJSON(w, http.StatusOK, ExpansionVars{}) 80 return 81 } 82 83 plugin.WriteJSON(w, http.StatusOK, projectVars.Vars) 84 return 85 } 86 87 // Execute fetches the expansions from the API server 88 func (self *FetchVarsCommand) Execute(pluginLogger plugin.Logger, 89 pluginCom plugin.PluginCommunicator, 90 conf *model.TaskConfig, 91 stop chan bool) error { 92 93 pluginLogger.LogTask(slogger.ERROR, "Expansions.fetch deprecated") 94 return nil 95 96 }