/usr/share/gocode/src/github.com/ctdk/goiardi/node/node.go is in golang-github-ctdk-goiardi-dev 0.11.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | /* Node object/class/whatever it is that Go calls them. */
/*
* Copyright (c) 2013-2016, Jeremy Bingham (<jeremy@goiardi.gl>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package node implements nodes. They do chef-client runs.
package node
import (
"database/sql"
"github.com/ctdk/goiardi/config"
"github.com/ctdk/goiardi/datastore"
"github.com/ctdk/goiardi/indexer"
"github.com/ctdk/goiardi/util"
"net/http"
)
// Node is a basic Chef node, holding the run list and attributes of the node.
type Node struct {
Name string `json:"name"`
ChefEnvironment string `json:"chef_environment"`
RunList []string `json:"run_list"`
JSONClass string `json:"json_class"`
ChefType string `json:"chef_type"`
Automatic map[string]interface{} `json:"automatic"`
Normal map[string]interface{} `json:"normal"`
Default map[string]interface{} `json:"default"`
Override map[string]interface{} `json:"override"`
isDown bool
}
// New makes a new node.
func New(name string) (*Node, util.Gerror) {
/* check for an existing node with this name */
if !util.ValidateDBagName(name) {
err := util.Errorf("Field 'name' invalid")
return nil, err
}
var found bool
if config.UsingDB() {
// will need redone if orgs ever get implemented
var err error
found, err = checkForNodeSQL(datastore.Dbh, name)
if err != nil {
gerr := util.Errorf(err.Error())
gerr.SetStatus(http.StatusInternalServerError)
return nil, gerr
}
} else {
ds := datastore.New()
_, found = ds.Get("node", name)
}
if found {
err := util.Errorf("Node %s already exists", name)
err.SetStatus(http.StatusConflict)
return nil, err
}
/* No node, we make a new one */
node := &Node{
Name: name,
ChefEnvironment: "_default",
ChefType: "node",
JSONClass: "Chef::Node",
RunList: []string{},
Automatic: map[string]interface{}{},
Normal: map[string]interface{}{},
Default: map[string]interface{}{},
Override: map[string]interface{}{},
}
return node, nil
}
// NewFromJSON creates a new node from the uploaded JSON.
func NewFromJSON(jsonNode map[string]interface{}) (*Node, util.Gerror) {
nodeName, nerr := util.ValidateAsString(jsonNode["name"])
if nerr != nil {
return nil, nerr
}
node, err := New(nodeName)
if err != nil {
return nil, err
}
err = node.UpdateFromJSON(jsonNode)
if err != nil {
return nil, err
}
return node, nil
}
// Get a node.
func Get(nodeName string) (*Node, util.Gerror) {
var node *Node
var found bool
if config.UsingDB() {
var err error
node, err = getSQL(nodeName)
if err != nil {
if err == sql.ErrNoRows {
found = false
} else {
return nil, util.CastErr(err)
}
} else {
found = true
}
} else {
ds := datastore.New()
var n interface{}
n, found = ds.Get("node", nodeName)
if n != nil {
node = n.(*Node)
}
}
if !found {
err := util.Errorf("node '%s' not found", nodeName)
err.SetStatus(http.StatusNotFound)
return nil, err
}
return node, nil
}
// GetMulti gets multiple nodes from a given slice of node names.
func GetMulti(nodeNames []string) ([]*Node, util.Gerror) {
var nodes []*Node
if config.UsingDB() {
var err error
nodes, err = getMultiSQL(nodeNames)
if err != nil && err != sql.ErrNoRows {
return nil, util.CastErr(err)
}
} else {
nodes = make([]*Node, 0, len(nodeNames))
for _, n := range nodeNames {
no, _ := Get(n)
if no != nil {
nodes = append(nodes, no)
}
}
}
return nodes, nil
}
// UpdateFromJSON updates an existing node with the uploaded JSON.
func (n *Node) UpdateFromJSON(jsonNode map[string]interface{}) util.Gerror {
/* It's actually totally legitimate to save a node with a different
* name than you started with, but we need to get/create a new node for
* it is all. */
nodeName, nerr := util.ValidateAsString(jsonNode["name"])
if nerr != nil {
return nerr
}
if n.Name != nodeName {
err := util.Errorf("Node name %s and %s from JSON do not match.", n.Name, nodeName)
return err
}
/* Validations */
/* Look for invalid top level elements. *We* don't have to worry about
* them, but chef-pedant cares (probably because Chef <=10 stores
* json objects directly, dunno about Chef 11). */
validElements := []string{"name", "json_class", "chef_type", "chef_environment", "run_list", "override", "normal", "default", "automatic"}
ValidElem:
for k := range jsonNode {
for _, i := range validElements {
if k == i {
continue ValidElem
}
}
err := util.Errorf("Invalid key %s in request body", k)
return err
}
var verr util.Gerror
jsonNode["run_list"], verr = util.ValidateRunList(jsonNode["run_list"])
if verr != nil {
return verr
}
attrs := []string{"normal", "automatic", "default", "override"}
for _, a := range attrs {
jsonNode[a], verr = util.ValidateAttributes(a, jsonNode[a])
if verr != nil {
return verr
}
}
jsonNode["chef_environment"], verr = util.ValidateAsFieldString(jsonNode["chef_environment"])
if verr != nil {
if verr.Error() == "Field 'name' nil" {
jsonNode["chef_environment"] = n.ChefEnvironment
} else {
return verr
}
} else {
if !util.ValidateEnvName(jsonNode["chef_environment"].(string)) {
verr = util.Errorf("Field 'chef_environment' invalid")
return verr
}
}
jsonNode["json_class"], verr = util.ValidateAsFieldString(jsonNode["json_class"])
if verr != nil {
if verr.Error() == "Field 'name' nil" {
jsonNode["json_class"] = n.JSONClass
} else {
return verr
}
} else {
if jsonNode["json_class"].(string) != "Chef::Node" {
verr = util.Errorf("Field 'json_class' invalid")
return verr
}
}
jsonNode["chef_type"], verr = util.ValidateAsFieldString(jsonNode["chef_type"])
if verr != nil {
if verr.Error() == "Field 'name' nil" {
jsonNode["chef_type"] = n.ChefType
} else {
return verr
}
} else {
if jsonNode["chef_type"].(string) != "node" {
verr = util.Errorf("Field 'chef_type' invalid")
return verr
}
}
/* and setting */
n.ChefEnvironment = jsonNode["chef_environment"].(string)
n.ChefType = jsonNode["chef_type"].(string)
n.JSONClass = jsonNode["json_class"].(string)
n.RunList = jsonNode["run_list"].([]string)
n.Normal = jsonNode["normal"].(map[string]interface{})
n.Automatic = jsonNode["automatic"].(map[string]interface{})
n.Default = jsonNode["default"].(map[string]interface{})
n.Override = jsonNode["override"].(map[string]interface{})
return nil
}
// Save the node.
func (n *Node) Save() error {
if config.UsingDB() {
if err := n.saveSQL(); err != nil {
return err
}
} else {
ds := datastore.New()
ds.Set("node", n.Name, n)
}
/* TODO Later: excellent candidate for a goroutine */
indexer.IndexObj(n)
return nil
}
// Delete the node.
func (n *Node) Delete() error {
if config.UsingDB() {
if err := n.deleteSQL(); err != nil {
return err
}
} else {
ds := datastore.New()
ds.Delete("node", n.Name)
// TODO: This may need a different config flag?
if config.Config.UseSerf {
n.deleteStatuses()
}
}
indexer.DeleteItemFromCollection("node", n.Name)
return nil
}
// GetList gets a list of the nodes on this server.
func GetList() []string {
var nodeList []string
if config.UsingDB() {
nodeList = getListSQL()
} else {
ds := datastore.New()
nodeList = ds.GetList("node")
}
return nodeList
}
// GetFromEnv returns all nodes that belong to the given environment.
func GetFromEnv(envName string) ([]*Node, error) {
if config.UsingDB() {
return getNodesInEnvSQL(envName)
}
var envNodes []*Node
nodeList := GetList()
for _, n := range nodeList {
chefNode, _ := Get(n)
if chefNode == nil {
continue
}
if chefNode.ChefEnvironment == envName {
envNodes = append(envNodes, chefNode)
}
}
return envNodes, nil
}
// GetName returns the node's name.
func (n *Node) GetName() string {
return n.Name
}
// URLType returns the base element of a node's URL.
func (n *Node) URLType() string {
return "nodes"
}
/* Functions to support indexing */
// DocID returns the node's name.
func (n *Node) DocID() string {
return n.Name
}
// Index tells the indexer where the node should go.
func (n *Node) Index() string {
return "node"
}
// Flatten a node for indexing.
func (n *Node) Flatten() map[string]interface{} {
return util.FlattenObj(n)
}
// AllNodes returns all the nodes on the server
func AllNodes() []*Node {
var nodes []*Node
if config.UsingDB() {
nodes = allNodesSQL()
} else {
nodeList := GetList()
for _, n := range nodeList {
no, err := Get(n)
if err != nil {
continue
}
nodes = append(nodes, no)
}
}
return nodes
}
// Count returns a count of all nodes on this server.
func Count() int64 {
if config.UsingDB() {
c, _ := countSQL()
return c
}
nl := GetList()
return int64(len(nl))
}
|