1 <?php
2 /**
3 * Copyright 2012-2014 Rackspace US, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace OpenCloud\LoadBalancer\Resource;
19
20 use OpenCloud\Common\Resource\PersistentResource;
21
22 /**
23 * The nodes defined by the load balancer are responsible for servicing the
24 * requests received through the load balancer's virtual IP. By default, the
25 * load balancer employs a basic health check that ensures the node is listening
26 * on its defined port. The node is checked at the time of addition and at regular
27 * intervals as defined by the load balancer health check configuration. If a
28 * back-end node is not listening on its port or does not meet the conditions of
29 * the defined active health check for the load balancer, then the load balancer
30 * will not forward connections and its status will be listed as "OFFLINE". Only
31 * nodes that are in an "ONLINE" status will receive and be able to service
32 * traffic from the load balancer.
33 *
34 * All nodes have an associated status that indicates whether the node is
35 * ONLINE, OFFLINE, or DRAINING. Only nodes that are in ONLINE status will
36 * receive and be able to service traffic from the load balancer. The OFFLINE
37 * status represents a node that cannot accept or service traffic. A node in
38 * DRAINING status represents a node that stops the traffic manager from sending
39 * any additional new connections to the node, but honors established sessions.
40 * If the traffic manager receives a request and session persistence requires
41 * that the node is used, the traffic manager will use it. The status is
42 * determined by the passive or active health monitors.
43 *
44 * If the WEIGHTED_ROUND_ROBIN load balancer algorithm mode is selected, then
45 * the caller should assign the relevant weights to the node as part of the
46 * weight attribute of the node element. When the algorithm of the load balancer
47 * is changed to WEIGHTED_ROUND_ROBIN and the nodes do not already have an
48 * assigned weight, the service will automatically set the weight to "1" for all nodes.
49 *
50 * One or more secondary nodes can be added to a specified load balancer so that
51 * if all the primary nodes fail, traffic can be redirected to secondary nodes.
52 * The type attribute allows configuring the node as either PRIMARY or SECONDARY.
53 */
54 class Node extends PersistentResource
55 {
56 public $id;
57
58 /**
59 * IP address or domain name for the node.
60 *
61 * @var string
62 */
63 public $address;
64
65 /**
66 * Port number for the service you are load balancing.
67 *
68 * @var int
69 */
70 public $port;
71
72 /**
73 * Condition for the node, which determines its role within the load balancer.
74 *
75 * @var string
76 */
77 public $condition;
78
79 /**
80 * Current state of the node. Can either be ONLINE, OFFLINE or DRAINING.
81 *
82 * @var string
83 */
84 public $status;
85
86 /**
87 * Weight of node to add. If the WEIGHTED_ROUND_ROBIN load balancer algorithm
88 * mode is selected, then the user should assign the relevant weight to the
89 * node using the weight attribute for the node. Must be an integer from 1 to 100.
90 *
91 * @var int
92 */
93 public $weight;
94
95 /**
96 * Type of node to add:
97 *
98 * * PRIMARY: Nodes defined as PRIMARY are in the normal rotation to receive
99 * traffic from the load balancer.
100 *
101 * * SECONDARY: Nodes defined as SECONDARY are only in the rotation to
102 * receive traffic from the load balancer when all the primary nodes fail.
103 *
104 * @var string
105 */
106 public $type;
107
108 protected static $json_name = false;
109 protected static $json_collection_name = 'nodes';
110 protected static $url_resource = 'nodes';
111
112 public $createKeys = array(
113 'address',
114 'port',
115 'condition',
116 'type',
117 'weight'
118 );
119
120 /**
121 * returns the Node name
122 *
123 * @return string
124 */
125 public function name()
126 {
127 return get_class() . '[' . $this->Id() . ']';
128 }
129
130 public function createJson()
131 {
132 $nodes = array('node' => array());
133
134 foreach ($this->createKeys as $key) {
135 $nodes['node'][$key] = $this->$key;
136 }
137
138 return array('nodes' => array($nodes));
139 }
140
141 protected function updateJson($params = array())
142 {
143 if ($this->condition) {
144 $params['condition'] = $this->condition;
145 }
146 if ($this->type) {
147 $params['type'] = $this->type;
148 }
149 if ($this->weight) {
150 $params['weight'] = $this->weight;
151 }
152
153 return (object) array('node' => (object) $params);
154 }
155
156 /**
157 * Returns a Metadata item
158 *
159 * @return Metadata
160 */
161 public function metadata($data = null)
162 {
163 return $this->getService()->resource('Metadata', $data, $this);
164 }
165
166 /**
167 * Returns a paginated collection of metadata
168 *
169 * @return PaginatedIterator
170 */
171 public function metadataList()
172 {
173 return $this->getService()->resourceList('Metadata', null, $this);
174 }
175 }
176