Completed
Pull Request — master (#487)
by
unknown
02:56
created

UpdateNodeCustomProperties   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 31
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 30 4
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
15
from lib.actions import OrionBaseAction
16
from lib.utils import send_user_error
17
18
19
class UpdateNodeCustomProperties(OrionBaseAction):
20
    def run(self, platform, node, custom_property, value):
21
        """
22
        Update a nodes Cutom Properties.
23
        """
24
        self.connect(platform)
25
26
        orion_node = self.get_node(node)
27
28
        if not orion_node.npm:
29
            msg = "Node ({}) does not exist".format(node)
30
            send_user_error(msg)
31
            raise ValueError(msg)
32
33
        current_properties = self.read(orion_node.uri + '/CustomProperties')
34
35
        if custom_property not in current_properties:
36
            msg = "custom property {} does not exist!".format(custom_property)
37
            send_user_error(msg)
38
            raise ValueError(msg)
39
40
        kargs = {custom_property: value}
41
42
        orion_data = self.update(orion_node.uri + '/CustomProperties', **kargs)
43
44
        # This update returns None, so check just in case.
45
        # This happens even if the custom_property does not exist!
46
        if orion_data is None:
47
            return True
48
        else:
49
            return orion_data
50