Move userdata from templates

This arrangement was driving me somewhat mad; make userdata just like
script in that it is not a "template".
This commit is contained in:
Joshua Boniface 2019-12-30 15:01:28 -05:00
parent d6bd616b31
commit e550d4589d
3 changed files with 131 additions and 124 deletions

View File

@ -170,20 +170,12 @@ def list_template_storage_disks(name):
disks = data['disks']
return disks
def list_template_userdata(limit, is_fuzzy=True):
"""
Obtain a list of userdata templates.
"""
data = list_template(limit, 'userdata_template', is_fuzzy)
return data
def template_list(limit):
system_templates = list_template_system(limit)
network_templates = list_template_network(limit)
storage_templates = list_template_storage(limit)
userdata_templates = list_template_userdata(limit)
return { "system_templates": system_templates, "network_templates": network_templates, "storage_templates": storage_templates, "userdata_templates": userdata_templates }
return { "system_templates": system_templates, "network_templates": network_templates, "storage_templates": storage_templates }
#
# Template Create functions
@ -317,49 +309,6 @@ def create_template_storage_element(name, pool, disk_id, disk_size_gb, filesyste
close_database(conn, cur)
return flask.jsonify(retmsg), retcode
def create_template_userdata(name, userdata):
if list_template_userdata(name, is_fuzzy=False):
retmsg = { "message": "The userdata template {} already exists".format(name) }
retcode = 400
return flask.jsonify(retmsg), retcode
conn, cur = open_database(config)
try:
query = "INSERT INTO userdata_template (name, userdata) VALUES (%s, %s);"
args = (name, userdata)
cur.execute(query, args)
retmsg = { "name": name }
retcode = 200
except psycopg2.IntegrityError as e:
retmsg = { "message": "Failed to create entry {}".format(name), "error": e }
retcode = 400
close_database(conn, cur)
return flask.jsonify(retmsg), retcode
#
# Template update functions
#
def update_template_userdata(name, userdata):
if not list_template_userdata(name, is_fuzzy=False):
retmsg = { "message": "The userdata template {} does not exist".format(name) }
retcode = 400
return flask.jsonify(retmsg), retcode
tid = list_template_userdata(name, is_fuzzy=False)[0]['id']
conn, cur = open_database(config)
try:
query = "UPDATE userdata_template SET userdata = %s WHERE id = %s;"
args = (userdata, tid)
cur.execute(query, args)
retmsg = { "name": name }
retcode = 200
except psycopg2.IntegrityError as e:
retmsg = { "message": "Failed to update entry {}".format(name), "error": e }
retcode = 400
close_database(conn, cur)
return flask.jsonify(retmsg), retcode
#
# Template Delete functions
#
@ -500,21 +449,89 @@ def delete_template_storage_element(name, disk_id):
close_database(conn, cur)
return flask.jsonify(retmsg), retcode
def delete_template_userdata(name):
if not list_template_userdata(name, is_fuzzy=False):
retmsg = { "message": "The userdata template {} does not exist".format(name) }
#
# Userdata functions
#
def list_userdata(limit, is_fuzzy=True):
if limit:
if is_fuzzy:
# Handle fuzzy vs. non-fuzzy limits
if not re.match('\^.*', limit):
limit = '%' + limit
else:
limit = limit[1:]
if not re.match('.*\$', limit):
limit = limit + '%'
else:
limit = limit[:-1]
query = "SELECT * FROM {} WHERE name LIKE %s;".format('userdata')
args = (limit, )
else:
query = "SELECT * FROM {};".format('userdata')
args = ()
conn, cur = open_database(config)
cur.execute(query, args)
data = cur.fetchall()
close_database(conn, cur)
return data
def create_userdata(name, userdata):
if list_userdata(name, is_fuzzy=False):
retmsg = { "message": "The userdata {} already exists".format(name) }
retcode = 400
return flask.jsonify(retmsg), retcode
conn, cur = open_database(config)
try:
query = "DELETE FROM userdata_template WHERE name = %s;"
query = "INSERT INTO userdata (name, userdata) VALUES (%s, %s);"
args = (name, userdata)
cur.execute(query, args)
retmsg = { "name": name }
retcode = 200
except psycopg2.IntegrityError as e:
retmsg = { "message": "Failed to create entry {}".format(name), "error": e }
retcode = 400
close_database(conn, cur)
return flask.jsonify(retmsg), retcode
def update_userdata(name, userdata):
if not list_userdata(name, is_fuzzy=False):
retmsg = { "message": "The userdata {} does not exist".format(name) }
retcode = 400
return flask.jsonify(retmsg), retcode
tid = list_userdata(name, is_fuzzy=False)[0]['id']
conn, cur = open_database(config)
try:
query = "UPDATE userdata SET userdata = %s WHERE id = %s;"
args = (userdata, tid)
cur.execute(query, args)
retmsg = { "name": name }
retcode = 200
except psycopg2.IntegrityError as e:
retmsg = { "message": "Failed to update entry {}".format(name), "error": e }
retcode = 400
close_database(conn, cur)
return flask.jsonify(retmsg), retcode
def delete_userdata(name):
if not list_userdata(name, is_fuzzy=False):
retmsg = { "message": "The userdata {} does not exist".format(name) }
retcode = 400
return flask.jsonify(retmsg), retcode
conn, cur = open_database(config)
try:
query = "DELETE FROM userdata WHERE name = %s;"
args = (name,)
cur.execute(query, args)
retmsg = { "name": name }
retcode = 200
except psycopg2.IntegrityError as e:
retmsg = { "message": "Failed to delete entry {}".format(name), "error": e }
retmsg = { "message": "Failed to delete entry {}".format(name), "error": str(e) }
retcode = 400
close_database(conn, cur)
return flask.jsonify(retmsg), retcode

View File

@ -3457,10 +3457,6 @@ class API_Provisioner_Template_Root(Resource):
type: array
items:
$ref: '#/definitions/storage-template'
userdata-templates:
type: array
items:
$ref: '#/definitions/userdata-template'
parameters:
- in: query
name: limit
@ -4538,29 +4534,29 @@ class API_Provisioner_Template_Storage_Disk_Element(Resource):
)
api.add_resource(API_Provisioner_Template_Storage_Disk_Element, '/provisioner/template/storage/<template>/disk/<disk_id>')
# /provisioner/template/userdata
class API_Provisioner_Template_Userdata_Root(Resource):
# /provisioner/userdata
class API_Provisioner_Userdata_Root(Resource):
@RequestParser([
{ 'name': 'limit' }
])
@Authenticator
def get(self, reqargs):
"""
Return a list of userdata templates
Return a list of userdata documents
---
tags:
- provisioner / template
- provisioner
definitions:
- schema:
type: object
id: userdata-template
id: userdata
properties:
id:
type: integer
description: Internal provisioner template ID
description: Internal provisioner ID
name:
type: string
description: Template name
description: Userdata name
userdata:
type: string
description: Raw userdata configuration document
@ -4569,36 +4565,36 @@ class API_Provisioner_Template_Userdata_Root(Resource):
name: limit
type: string
required: false
description: A template name search limit; fuzzy by default, use ^/$ to force exact matches
description: A userdata name search limit; fuzzy by default, use ^/$ to force exact matches
responses:
200:
description: OK
schema:
type: list
items:
$ref: '#/definitions/userdata-template'
$ref: '#/definitions/userdata'
"""
return api_provisioner.list_template_userdata(
reqargs.get('limit', None)
)
@RequestParser([
{ 'name': 'name', 'required': True, 'helpmsg': "A template name must be specified" },
{ 'name': 'name', 'required': True, 'helpmsg': "A name must be specified" },
{ 'name': 'data', 'required': True, 'helpmsg': "A userdata document must be specified" }
])
@Authenticator
def post(self, reqargs):
"""
Create a new userdata template
Create a new userdata document
---
tags:
- provisioner / template
- provisioner
parameters:
- in: query
name: name
type: string
required: true
description: Template name
description: Userdata name
- in: query
name: data
type: string
@ -4616,33 +4612,33 @@ class API_Provisioner_Template_Userdata_Root(Resource):
type: object
id: Message
"""
return api_provisioner.create_template_userdata(
return api_provisioner.create_userdata(
reqargs.get('name', None),
reqargs.get('data', None)
)
api.add_resource(API_Provisioner_Template_Userdata_Root, '/provisioner/template/userdata')
api.add_resource(API_Provisioner_Userdata_Root, '/provisioner/userdata')
# /provisioner/template/userdata/<template>
class API_Provisioner_Template_Userdata_Element(Resource):
# /provisioner/userdata/<userdata>
class API_Provisioner_Userdata_Element(Resource):
@Authenticator
def get(self, template):
def get(self, userdata):
"""
Return information about userdata template {template}
Return information about userdata document {userdata}
---
tags:
- provisioner / template
- provisioner
responses:
200:
description: OK
schema:
$ref: '#/definitions/userdata-template'
$ref: '#/definitions/userdata'
404:
description: Not found
schema:
type: object
id: Message
"""
return api_provisioner.list_template_userdata(
return api_provisioner.list_userdata(
template,
is_fuzzy=False
)
@ -4651,12 +4647,12 @@ class API_Provisioner_Template_Userdata_Element(Resource):
{ 'name': 'data', 'required': True, 'helpmsg': "A userdata document must be specified" }
])
@Authenticator
def post(self, template, reqargs):
def post(self, userdata, reqargs):
"""
Create a new userdata template {template}
Create a new userdata document {userdata}
---
tags:
- provisioner / template
- provisioner
parameters:
- in: query
name: data
@ -4675,8 +4671,8 @@ class API_Provisioner_Template_Userdata_Element(Resource):
type: object
id: Message
"""
return api_provisioner.create_template_userdata(
template,
return api_provisioner.create_userdata(
userdata,
reqargs.get('data', None)
)
@ -4686,10 +4682,10 @@ class API_Provisioner_Template_Userdata_Element(Resource):
@Authenticator
def put(self, template):
"""
Update userdata template {template}
Update userdata document {userdata}
---
tags:
- provisioner / template
- provisioner
parameters:
- in: query
name: data
@ -4708,18 +4704,18 @@ class API_Provisioner_Template_Userdata_Element(Resource):
type: object
id: Message
"""
return api_provisioner.update_template_userdata(
template,
return api_provisioner.update_userdata(
userdata,
reqargs.get('data', None)
)
@Authenticator
def delete(self, template):
"""
Remove userdata template {template}
Remove userdata document {userdata}
---
tags:
- provisioner / template
- provisioner
responses:
200:
description: OK
@ -4732,10 +4728,10 @@ class API_Provisioner_Template_Userdata_Element(Resource):
type: object
id: Message
"""
return api_provisioner.delete_template_userdata(
template
return api_provisioner.delete_userdata(
userdata
)
api.add_resource(API_Provisioner_Template_Userdata_Element, '/provisioner/template/userdata/<template>')
api.add_resource(API_Provisioner_Userdata_Element, '/provisioner/userdata/<userdata>')
# /provisioner/script
class API_Provisioner_Script_Root(Resource):

View File

@ -243,12 +243,6 @@
"$ref": "#/definitions/system-template"
},
"type": "array"
},
"userdata-templates": {
"items": {
"$ref": "#/definitions/userdata-template"
},
"type": "array"
}
},
"type": "object"
@ -722,14 +716,14 @@
},
"type": "object"
},
"userdata-template": {
"userdata": {
"properties": {
"id": {
"description": "Internal provisioner template ID",
"description": "Internal provisioner ID",
"type": "integer"
},
"name": {
"description": "Template name",
"description": "Userdata name",
"type": "string"
},
"userdata": {
@ -3357,12 +3351,12 @@
]
}
},
"/api/v1/provisioner/template/userdata": {
"/api/v1/provisioner/userdata": {
"get": {
"description": "",
"parameters": [
{
"description": "A template name search limit; fuzzy by default, use ^/$ to force exact matches",
"description": "A userdata name search limit; fuzzy by default, use ^/$ to force exact matches",
"in": "query",
"name": "limit",
"required": false,
@ -3374,22 +3368,22 @@
"description": "OK",
"schema": {
"items": {
"$ref": "#/definitions/userdata-template"
"$ref": "#/definitions/userdata"
},
"type": "list"
}
}
},
"summary": "Return a list of userdata templates",
"summary": "Return a list of userdata documents",
"tags": [
"provisioner / template"
"provisioner"
]
},
"post": {
"description": "",
"parameters": [
{
"description": "Template name",
"description": "Userdata name",
"in": "query",
"name": "name",
"required": true,
@ -3417,13 +3411,13 @@
}
}
},
"summary": "Create a new userdata template",
"summary": "Create a new userdata document",
"tags": [
"provisioner / template"
"provisioner"
]
}
},
"/api/v1/provisioner/template/userdata/{template}": {
"/api/v1/provisioner/userdata/{userdata}": {
"delete": {
"description": "",
"responses": {
@ -3440,9 +3434,9 @@
}
}
},
"summary": "Remove userdata template {template}",
"summary": "Remove userdata document {userdata}",
"tags": [
"provisioner / template"
"provisioner"
]
},
"get": {
@ -3451,7 +3445,7 @@
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/userdata-template"
"$ref": "#/definitions/userdata"
}
},
"404": {
@ -3461,9 +3455,9 @@
}
}
},
"summary": "Return information about userdata template {template}",
"summary": "Return information about userdata document {userdata}",
"tags": [
"provisioner / template"
"provisioner"
]
},
"post": {
@ -3491,9 +3485,9 @@
}
}
},
"summary": "Create a new userdata template {template}",
"summary": "Create a new userdata document {userdata}",
"tags": [
"provisioner / template"
"provisioner"
]
},
"put": {
@ -3521,9 +3515,9 @@
}
}
},
"summary": "Update userdata template {template}",
"summary": "Update userdata document {userdata}",
"tags": [
"provisioner / template"
"provisioner"
]
}
},