2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bluemix IaaS(旧SoftLayer)の見積もりをAPI経由でPDFに保存する

Last updated at Posted at 2017-01-30

#内容
すでにポータル上に見積もりとして存在するレコードをAPI経由でPDF保存してみました。

#前提
以下の記事が理解できていることが前提です。
Bluemix Infrastructure (旧SoftLayer) のAPIについて、詳しく解説してあります。

ShinobiLayer: SoftLayer API 次の一歩: データ型オブジェクト(1) - Qiita

#QuoteIdの取得
見積もりのIDを取得するスクリプトをつくったので、ぜひ使ってください。

##スクリプト

getQuotes.py
#import package
import SoftLayer
import json
from prettytable import PrettyTable

# account info
client = SoftLayer.create_client_from_env()

objectmask = """
    id,
    name,
    status,
    createDate,
    expirationDate,
    order[
        id,
        orderTotalAmount
    ]
"""

# getActiveQuotes
gAQ = client['Account'].getActiveQuotes(mask=objectmask)

# define table
table = PrettyTable([
    'QuoteID',
    'Name',
    'Amount',
    'Status',
    'createDate',
    'expirationDate'
])


for key in range(len(gAQ)):
        table.add_row([
            gAQ[key]['id'],
            gAQ[key]['name'],
            gAQ[key]['order'].get('orderTotalAmount',""),
            gAQ[key]['status'],
            gAQ[key]['createDate'],
            gAQ[key]['expirationDate']
        ])

print len(gAQ)
print table.get_string(sortby="QuoteID")

##実行結果の例

#実行コマンド
python getQuotes.py 

#結果
5
+---------+-------------+---------+---------+---------------------------+---------------------------+
| QuoteID |     Name    |  Amount |  Status |         createDate        |       expirationDate      |
+---------+-------------+---------+---------+---------------------------+---------------------------+
| 1111111 | BMS_28Cores | 1402.31 | PENDING | 2016-11-16T10:51:51+09:00 | 2017-02-14T10:51:51+09:00 |
...

#PDFとして保存
QuoteIdを指定して、PDFとして保存できるスクリプトをつくったので、ぜひ使ってください。

##スクリプト

getPdf.py
# beginning message
print 'Saving the quote as PDF ...'

# import
import SoftLayer
import sys
parm=sys.argv
quoteId=parm[1]

# account info
client = SoftLayer.create_client_from_env()

# getPdf as a binary data
getPdf = client['Billing_Order_Quote'].getPdf(id=quoteId)

# confirm as xmlrpclib.Binary
# print getPdf.__class__

# Save as a PDF
quoteFileName = "Quote_ID_%s.pdf" % quoteId
w = open(quoteFileName, "wb")
w.write(getPdf.data)
w.close()

# finishing message
print 'Completed!'

##実行結果の例

#実行コマンド
python getPdf.py 1111111

#結果
Saving the quote as PDF ...
Completed!

#「Quote_ID_1111111.pdf」という名前で保存されます

#参考にしたサイト

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?