summaryrefslogtreecommitdiff
path: root/reactive/dynect_provider.py
blob: d1629e7ad40e090b17f5388c3a4f7bfa13951bd7 (plain)
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
#!/usr/bin/env python3
#
# Copyright:    (c) 2016 Canonical
# License:      GPLv3 <http://www.gnu.org/licenses/gpl.html>


# import ipaddress
# import pprint
import os
import sys

# Augment system path.
if 'JUJU_CHARM_DIR' in os.environ:
    dirs = ['lib', 'reactive']
    for d in dirs:
        reactivedir = os.path.join(os.environ['JUJU_CHARM_DIR'], d)
        abspath = os.path.abspath(reactivedir)
        sys.path.insert(0, abspath)

from charmhelpers.core import (                 # NOQA: E402
    hookenv,
    unitdata,
)

from charms.reactive import (                   # NOQA: E402
    main,
    remove_state,
    set_state,
)

from charms.reactive.decorators import (        # NOQA: E402
    when,
    when_all,
    when_any,
    when_not,
    when_not_all,
)

from dns_common import (                        # NOQA: E402
    active,
    blocked,
    maint,
)

"""
from dynect.DynectDNS import DynectRest     # NOQA: E402

rest_iface = DynectRest()

# Log in
arguments = {
    'customer_name': 'my_cust',
    'user_name': 'my_user',
    'password': 'my_pass',
}
response = rest_iface.execute('/Session/', 'POST', arguments)

if response['status'] != 'success':
    sys.exit("Incorrect credentials")

# Perform action
response = rest_iface.execute('/Zone/', 'GET')
zone_resources = response['data']

# Log out, to be polite
rest_iface.execute('/Session/', 'DELETE')
"""


@when_all(
    'dns-auto.peers-available',
    'dynect.configured',
    'dynect.enabled',
    'leadership.is_leader',
)
def update_dynect():
    db = unitdata.kv()
    peers = db.get('peers')
    active('Ready to update dynect with: %s' % peers)


@when_all(
    'dynect.configured',
    'dynect.enabled',
    'leadership.is_leader',
)
@when_not('dns-auto.peers-available')
def no_peers_available():
    blocked('Waiting on dns-auto peers to be available before we update dynect')


@when_all(
    'dynect.enabled',
    'leadership.is_leader',
)
@when_not('dynect.configured')
def config_missing():
    blocked('Mandatory config missing - you must provide customer_name, user_name, and password to configure Dynect')


@when('dynect.enabled')
@when_not('leadership.is_leader')
def do_nothing():
    blocked('Non-leader unit; doing nothing')


@when('config.changed')
def check_config():
    """
    Do not activate unless provider is set to dynect
    and all required config is set.
    """
    provider = hookenv.config('provider')
    if provider != 'dynect':
        hookenv.log('Provider is %s; not activating dynect' % (provider,))
        remove_state('dynect.enabled')
    else:
        set_state('dynect.enabled')

    cfg = ''
    for option in ['customer_name', 'user_name', 'password']:
        cfg = hookenv.config(option)
        if cfg is None or cfg == '':
            remove_state('dynect.configured')
            return
    set_state('dynect.configured')


if __name__ == '__main__':
    main()