Skip to content

Commit

Permalink
Merge pull request #74 from CiscoDevNet/ccp-v3-api
Browse files Browse the repository at this point in the history
Ccp v3 api
  • Loading branch information
3191110276 authored Feb 28, 2020
2 parents 5372c64 + fed5bea commit a620e1e
Show file tree
Hide file tree
Showing 61 changed files with 2,425 additions and 1,560 deletions.
99 changes: 43 additions & 56 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set ENV VARS
ENV FLASK_ENV=development
ENV PYTHONUNBUFFERED=0
ENV PLATFORM=linux
ENV K8SVERSION=1.14.0
ENV KS_VERSION=0.13.1
ENV KF_VERSION=0.5.1

# Set the working directory to /app
WORKDIR /app


# Install for ssh-keygen
RUN apt-get update && apt-get install -y \
curl \
wget \
openssh-client

# Install needed executables
RUN wget https://storage.googleapis.com/kubernetes-release/release/v${K8SVERSION}/bin/${PLATFORM}/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/

RUN wget https://github.com/ksonnet/ksonnet/releases/download/v${KS_VERSION}/ks_${KS_VERSION}_${PLATFORM}_amd64.tar.gz
RUN tar -xvf ks_${KS_VERSION}_${PLATFORM}_amd64.tar.gz
RUN rm ks_${KS_VERSION}_${PLATFORM}_amd64.tar.gz
RUN chmod +x ks_${KS_VERSION}_${PLATFORM}_amd64/ks
RUN mv ks_${KS_VERSION}_${PLATFORM}_amd64/ks /usr/local/bin/
RUN rm -rf ks_${KS_VERSION}_${PLATFORM}_amd64/

RUN wget https://github.com/kubeflow/kubeflow/releases/download/v${KF_VERSION}/kfctl_v${KF_VERSION}_${PLATFORM}.tar.gz
RUN tar -xvf kfctl_v${KF_VERSION}_${PLATFORM}.tar.gz
RUN rm kfctl_v${KF_VERSION}_${PLATFORM}.tar.gz
RUN chmod +x ./kfctl
RUN mv ./kfctl /usr/local/bin/



# Copy the current directory contents into the container at /app
COPY . /app


# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

WORKDIR /app/mla_app_code
RUN chmod +x helmInstall.sh
RUN ./helmInstall.sh


# Run amla_core_code.py when the container launches
CMD python "./mla_core_code.py"

# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set ENV VARS
ENV FLASK_ENV=development
ENV PYTHONUNBUFFERED=0
ENV PLATFORM=linux
ENV K8SVERSION=1.14.0

# Set the working directory to /app
WORKDIR /app


# Install for ssh-keygen
RUN apt-get update && apt-get install -y \
curl \
wget


# Install kubectl
RUN wget https://storage.googleapis.com/kubernetes-release/release/v${K8SVERSION}/bin/${PLATFORM}/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/


#Upgrade pip
RUN pip install --upgrade pip


# Copy the current directory contents into the container at /app
COPY . /app


# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt


WORKDIR /app/mla_app_code


# Run amla_core_code.py when the container launches
CMD python "./mla_core_code.py"

160 changes: 144 additions & 16 deletions mla_app_code/ccp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,129 @@
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

class CCP:
def __init__(self, url=None, username=None,password=None,cookie=None):
def __init__(self, url=None, username=None,password=None,cookie=None, token=None, apiVersion = 3):
self.url = url
self.username = username
self.password = password
self.cookie = cookie
self.token = token
self.apiVersion = apiVersion

def login(self):
def loginV2(self):

headers = {
'content-type': 'application/json',
}

# vip pools UUID still requires the v2 API so you need to login for both the v2 and v3 API

response = requests.request("POST", self.url + "/2/system/login?username=" + self.username + "&password=" + self.password, headers=headers, verify=False)

if response:
self.cookie = response.cookies

return response


def loginV3(self):

headers = {
'content-type': 'application/json',
}

# vip pools UUID still requires the v2 API so you need to login for both the v2 and v3 API

data = {"username":self.username,"password":self.password}

response = requests.request("POST", self.url + "/v3/system/login",data=json.dumps(data), headers=headers, verify=False)

if "X-Auth-Token" in response.headers:
self.token = response.headers["X-Auth-Token"]
return response.headers["X-Auth-Token"]

return None


def getConfig(self, uuid):

response = requests.request("GET", self.url + "/2/clusters/" + uuid + "/env", cookies=self.cookie, verify=False)

headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/clusters/" + uuid + "/env", cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/clusters/" + uuid ,headers=headers, verify=False)

return response




def getClusters(self):

response = requests.request("GET", self.url + "/2/clusters",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/clusters",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/clusters",headers=headers, verify=False)

return response

def getCluster(self,name):

response = requests.request("GET", self.url + "/2/clusters/"+name,cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/clusters/"+name,cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/clusters/"+name,headers=headers, verify=False)

return response


def getProviderClientConfigs(self):

response = requests.request("GET", self.url + "/2/providerclientconfigs",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

return response
# the v2 API had a "uuid" attribute while the v3 API has renamed this to "id". for the v3 API I'm adding a "uuid" key to the response (newProvidersList)
# so that we don't have to update the front end calls as it's expecting "uuid", not "id"

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/providerclientconfigs",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/providers",headers=headers, verify=False)
if response:
newProviderList = []
for provider in response.json():
provider["uuid"] = provider["id"]
newProviderList.append(provider)
return newProviderList

return response.text

def getProviderVsphereDatacenters(self,providerClientUUID):

response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/providers/" + providerClientUUID + "/datacenters",headers=headers, verify=False)

response = response.json()

Expand All @@ -82,8 +158,17 @@ def getProviderVsphereDatacenters(self,providerClientUUID):

def getProviderVsphereClusters(self,providerClientUUID,datacenterName):

response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/cluster",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/cluster",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/providers/" + providerClientUUID + "/clusters?datacenter="+datacenterName,headers=headers, verify=False)


response = response.json()

if "Clusters" in response:
Expand All @@ -93,7 +178,15 @@ def getProviderVsphereClusters(self,providerClientUUID,datacenterName):

def getProviderVsphereNetworks(self,providerClientUUID,datacenterName):

response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/network",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/network",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/providers/" + providerClientUUID + "/networks?datacenter="+datacenterName,headers=headers, verify=False)

response = response.json()

Expand All @@ -104,7 +197,15 @@ def getProviderVsphereNetworks(self,providerClientUUID,datacenterName):

def getProviderVsphereVMs(self,providerClientUUID,datacenterName):

response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/vm",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/vm",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/providers/" + providerClientUUID + "/vms?datacenter="+datacenterName,headers=headers, verify=False)

response = response.json()

Expand All @@ -115,7 +216,15 @@ def getProviderVsphereVMs(self,providerClientUUID,datacenterName):

def getProviderVsphereDatastores(self,providerClientUUID,datacenterName):

response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/datastore",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/datastore",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/providers/" + providerClientUUID + "/datastores?datacenter="+datacenterName,headers=headers, verify=False)

response = response.json()

Expand All @@ -126,7 +235,15 @@ def getProviderVsphereDatastores(self,providerClientUUID,datacenterName):

def getProviderVsphereResourcePools(self,providerClientUUID,datacenterName,clusterName):

response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/cluster/" + clusterName + "/pool",cookies=self.cookie, verify=False)
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

if self.apiVersion == 2:
response = requests.request("GET", self.url + "/2/providerclientconfigs/" + providerClientUUID + "/vsphere/datacenter/" + datacenterName + "/cluster/" + clusterName + "/pool",cookies=self.cookie, verify=False)
else:
response = requests.request("GET", self.url + "/v3/providers/" + providerClientUUID + "/resource-pools?datacenter="+datacenterName,headers=headers, verify=False)

response = response.json()

Expand All @@ -138,7 +255,7 @@ def getProviderVsphereResourcePools(self,providerClientUUID,datacenterName,clust
def getVIPPools(self):

response = requests.request("GET", self.url + "/2/network_service/subnets",cookies=self.cookie, verify=False)

response = response.json()

if "Pools" in response:
Expand All @@ -150,9 +267,20 @@ def deployCluster(self, newClusterDetails):

headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}

response = requests.request("POST", self.url + "/2/clusters", json=newClusterDetails,cookies=self.cookie, headers=headers, verify=False)
if self.apiVersion == 2:
headers = {
'content-type': 'application/json',
}
response = requests.request("POST", self.url + "/2/clusters", json=newClusterDetails,cookies=self.cookie, headers=headers, verify=False)
else:
headers = {
'content-type': 'application/json',
'x-auth-token': self.token,
}
response = requests.request("POST", self.url + "/v3/clusters/", json=newClusterDetails, headers=headers, verify=False)

return response

Expand Down
Loading

0 comments on commit a620e1e

Please sign in to comment.