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'] disks = data['disks']
return 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): def template_list(limit):
system_templates = list_template_system(limit) system_templates = list_template_system(limit)
network_templates = list_template_network(limit) network_templates = list_template_network(limit)
storage_templates = list_template_storage(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 # Template Create functions
@ -317,49 +309,6 @@ def create_template_storage_element(name, pool, disk_id, disk_size_gb, filesyste
close_database(conn, cur) close_database(conn, cur)
return flask.jsonify(retmsg), retcode 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 # Template Delete functions
# #
@ -500,21 +449,89 @@ def delete_template_storage_element(name, disk_id):
close_database(conn, cur) close_database(conn, cur)
return flask.jsonify(retmsg), retcode return flask.jsonify(retmsg), retcode
def delete_template_userdata(name): #
if not list_template_userdata(name, is_fuzzy=False): # Userdata functions
retmsg = { "message": "The userdata template {} does not exist".format(name) } #
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 retcode = 400
return flask.jsonify(retmsg), retcode return flask.jsonify(retmsg), retcode
conn, cur = open_database(config) conn, cur = open_database(config)
try: 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,) args = (name,)
cur.execute(query, args) cur.execute(query, args)
retmsg = { "name": name } retmsg = { "name": name }
retcode = 200 retcode = 200
except psycopg2.IntegrityError as e: 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 retcode = 400
close_database(conn, cur) close_database(conn, cur)
return flask.jsonify(retmsg), retcode return flask.jsonify(retmsg), retcode

View File

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

View File

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