CLI Reference Guide
from base64 import b64encode
import mimetypes
import sys
def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
def get_file_contents(filename):
CRLF = '\r\n'
f = open(filename, 'r')
return CRLF.join(f.readlines())
def encode_multipart_formdata(filename):
BOUNDARY = '----------CSA_r0ck$_$'
CRLF = '\r\n'
L = []
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="file"; filename="%s"' % file
name)
L.append('Content-Type: %s' % get_content_type(filename))
L.append('')
L.append(get_file_contents(filename))
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
def addDocumentToOffering(offeringId, documentName, userId):
conn = HTTPSConnection("localhost:8444")
userAndPass = b64encode(b"admin:cloud").decode("ascii")
post_url = "/csa/rest/artifact/" + offeringId + "/document?userIdentifier="+
userId
content_type, body = encode_multipart_formdata(documentName)
content_length = str(len(body))
headers = {'Authorization' : 'Basic %s' % userAndPass,
'Content-Type' :'%s' % content_type, 'content-length' : str(len(b
ody)), 'Accept' : 'application/xml'}
conn.request('POST', post_url, body, headers=headers)
res = conn.getresponse()
data = res.read()
print(data)
def main(offeringId, documentName, userId):
addDocumentToOffering(offeringId, documentName, userId)
if __name__ == "__main__":
offeringId = sys.argv[1]
documentName = sys.argv[2]
userId = sys.argv[3]
main(offeringId, documentName, userId)
HP Cloud Service Automation (4.00)Page 29 of 141
APIReference
Contents