summaryrefslogtreecommitdiffstats
path: root/clover/controller/control/api/halyard.py
blob: 861de5955101a7fe9d38e0efc2cc104760442fda (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
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
# Copyright (c) Authors of Clover
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

from flask import Blueprint, request, Response
import logging
import lib.halyard_base as base

halyard = Blueprint('halyard', __name__)

@halyard.route("/halyard/addkube", methods=['GET', 'POST'])
def addkubernetes():
    try:
        p = request.json
        accountname = p['Name']
        providerversion = p['ProviderVersion']
        Registries = p['DockerRegistries']
        kubeconfigFile = p['KubeconfigFile']

    except (KeyError, ValueError) as e:
        logging.debug(e)
        return Response('Invalid value in kubernetes yaml', status=400)

    try:
        if base.is_account_exist("kubernetes",accountname):
            return Response("account name has already exist", status=400)
        if providerversion == None or providerversion == 'V1':
            providerversion = None
            if 0 == len(Registries) or isinstance(Registries, list) == False:
                return Response("V1 provider require dockerRegistries", status=400)

        dockerRegistries = []
        for registry in Registries:
            registryname = registry['AccountName']
            if not base.is_account_exist("dockerRegistry",registryname):
                return Response("docker registry: {0} don't exist".format(registryname),
                         status=400)
            docker_dict = {"accountName":registryname, "namespaces":[]}
            dockerRegistries.append(docker_dict)
        data = {
                "name": accountname,
                "requiredGroupMembership": [],
                "providerVersion": providerversion,
                "permissions": {},
                "dockerRegistries": dockerRegistries,
                "context": None,
                "cluster": None,
                "user": None,
                "configureImagePullSecrets": "true",
                "serviceAccount": None,
                "cacheThreads": 1,
                "namespaces": [],
                "omitNamespaces": [],
                "kinds": [],
                "omitKinds": [],
                "customResources": [],
                "cachingPolicies": [],
                "kubeconfigFile": kubeconfigFile,
                "kubeconfigContents": None,
                "kubectlPath": None,
                "namingStrategy": None,
                "skin": None,
                "debug": None,
                "oauthScopes": [],
                "oauthServiceAccount": None,
                "oAuthServiceAccount": None,
                "oAuthScopes": []
              }
        result = base.add_account("kubernetes",data)
    except Exception as e:
        logging.debug(e)
        return Response('Failed add the kubernetes provider', status=400)
    return result

@halyard.route("/halyard/addregistry", methods=['GET', 'POST'])
def add_docker_registry():
    try:
        p = request.json
        accountname = p['name']
        address = p['address']
        repositories = p['repositories']
        if p.has_key('username') and p.has_key('password'):
            username = p['username']
            password = p['password']
        else:
            username = None
            password = None

    except (KeyError, ValueError) as e:
        logging.debug(e)
        return Response('Invalid value in kubernetes yaml', status=400)

    try:
        if base.is_account_exist("dockerRegistry",accountname):
            return Response("account name has already exist", status=400)

        data = {
                "name": accountname,
                "requiredGroupMembership": [],
                "providerVersion": None,
                "permissions": {},
                "address": address,
                "username": username,
                "password": password,
                "email": "fake.email@spinnaker.io",
                "cacheIntervalSeconds": 30,
                "clientTimeoutMillis": 60000,
                "cacheThreads": 1,
                "paginateSize": 100,
                "sortTagsByDate": False,
                "trackDigests": False,
                "insecureRegistry": False,
                "repositories": repositories,
                "passwordFile": None,
                "dockerconfigFile": None
              }
        result = base.add_account("dockerRegistry",data)
        if result != "SUCCEEDED":
            return Response('Failed to add the docker registry', status=400)

    except Exception as e:
        logging.debug(e)
        return Response('Failed to add the docker registry', status=400)
    return result

@halyard.route("/halyard/delprovider", methods=['GET', 'POST'])
def delprovider():
    try:
        p = request.json
        provider = p['provider']
        name = p['name']
    except (KeyError, ValueError) as e:
        logging.debug(e)
        return Response('Input invalid value', status=400)
    try:
        result = base.delete_account(provider, name)
        if result != "SUCCEEDED":
            print "Delete account failed"
            return Response('Failed to delete the {0} provider'.format(provider), status=400)

        apply_result = base.apply_deploy()
        if apply_result != "SUCCEEDED":
            print "Delete account failed"
            return Response('Failed to delete the {0} provider'.format(provider), status=400)

    except Exception as e:
        logging.debug(e)
        return Response('Failed to delete the kubernetes provider', status=400)

    return apply_result


@halyard.route("/halyard/account", methods=['GET', 'POST'])
def getprovider():
    try:
        provider = ""
        p = request.json
        provider = p['name']
        account_list = base.list_accounts(provider)
        result = ':'.join(account_list)
    except Exception as e:
        logging.debug(e)
        return Response('get {0} failed'.format(provider), status=400)
    return result