Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #337 from HewlettPackard/update_scmb_examples
Browse files Browse the repository at this point in the history
Updates SCMB examples replacing legacy code
  • Loading branch information
tmiotto authored Nov 13, 2017
2 parents c0708ec + 47a29e9 commit 99dbd76
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 67 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#### New Resources:
- Version

#### Bug fixes & Enhancements
- [#332](https://github.com/HewlettPackard/python-hpOneView/issues/332) example scmb.py is broken with v4.x libray

# 4.3.0
#### Notes
Added endpoints-support.md to track the supported and tested endpoints for the different HPE OneView REST APIs
Expand Down
84 changes: 47 additions & 37 deletions examples/scmb/ov_to_sm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@
# THE SOFTWARE.
###

from hpOneView import *
from hpOneView.oneview_client import OneViewClient
from base64 import b64encode
from functools import partial
from pprint import pprint

import amqp
import amqp.spec
import datetime
import http
import json
import ssl
import datetime
from pprint import pprint
from base64 import b64encode
import time

smhost = None
smhead = None
act = None


def sm_do_http(method, path, body):
Expand Down Expand Up @@ -101,23 +103,32 @@ def new_incident(desc, sev):


def print_alert(uri):
alerts = act.get_alerts()
alerts = oneview_client.alerts.get_all()
for alert in alerts:
if alert['uri'] == uri:
pprint(alert)


def update_alert(uri, smid):
alerts = act.get_alerts()
alerts = oneview_client.alerts.get_all()
notes = 'Case automatically loged in HPE Service Manager with ID: ' + smid
for alert in alerts:
if alert['uri'] == uri:
amap = common.make_alertMap_dict(notes, alert['eTag'])
act.update_alert(alert, amap)
oneview_client.alerts.update(create_alert_map(notes, alert['eTag']), alert['uris'])
return True
return False


def create_alert_map(notes, etag):
return {
'alertState': 'Active',
'assignedToUser': 'None',
'alertUrgency': 'None',
'notes': notes,
'eTag': etag
}


def get_incidents():
body = sm_get('/SM/9/rest/incidents?&view=expand')
pprint(body)
Expand Down Expand Up @@ -222,38 +233,34 @@ def recv(host, route):
conn.close()


def login(con, credential):
# Login with givin credentials
try:
con.login(credential)
except:
print('Login failed')


def acceptEULA(con):
def acceptEULA(oneview_client):
# See if we need to accept the EULA before we try to log in
con.get_eula_status()
eula_status = oneview_client.connection.get_eula_status()
try:
if con.get_eula_status() is True:
con.set_eula('no')
if eula_status is True:
oneview_client.connection.set_eula('no')
except Exception as e:
print('EXCEPTION:')
print(e)


def getCertCa(sec):
cert = sec.get_cert_ca()
def getCertCa(oneview_client):
cert = oneview_client.certificate_authority.get()
ca = open('caroot.pem', 'w+')
ca.write(cert)
ca.close()


def genRabbitCa(sec):
sec.gen_rabbitmq_ca()
def genRabbitCa(oneview_client):
certificate_ca_signed_client = {
"commonName": "default",
"type": "RabbitMqClientCertV2"
}
oneview_client.certificate_rabbitmq.generate(certificate_ca_signed_client)


def getRabbitKp(sec):
cert = sec.get_rabbitmq_kp()
def getRabbitKp(oneview_client):
cert = oneview_client.certificate_rabbitmq.get_key_pair('default')
ca = open('client.pem', 'w+')
ca.write(cert['base64SSLCertData'])
ca.close()
Expand All @@ -263,7 +270,7 @@ def getRabbitKp(sec):


def main():
global smhost, smhead, act
global smhost, smhead, oneview_client

if amqp.VERSION < (2, 1, 4):
print("WARNING: This script has been tested only with amqp 2.1.4, "
Expand Down Expand Up @@ -294,7 +301,6 @@ def main():
action='store_true',
help='List all Service Manager incidents and exit')
args = parser.parse_args()
credential = {'userName': args.user, 'password': args.passwd}
smcred = args.id + ':' + args.spass
userAndPass = b64encode(str.encode(smcred)).decode('ascii')
smhead = {'Content-Type': 'application/json;charset=utf-8',
Expand All @@ -305,21 +311,25 @@ def main():
get_incidents()
sys.exit()

con = connection(args.host)
sec = security(con)
act = activity(con)
config = {
"ip": args.host,
"credentials": {
"userName": args.user,
"password": args.passwd
}
}

login(con, credential)
acceptEULA(con)
oneview_client = OneViewClient(config)
acceptEULA(oneview_client)

# Generate the RabbitMQ keypair (only needs to be done one time)
if args.gen:
genRabbitCa(sec)
genRabbitCa(oneview_client)
sys.exit()

if args.down:
getCertCa(sec)
getRabbitKp(sec)
getCertCa(oneview_client)
getRabbitKp(oneview_client)
sys.exit()

recv(args.host, args.route)
Expand Down
58 changes: 29 additions & 29 deletions examples/scmb/scmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
# THE SOFTWARE.
###

from hpOneView import *
from hpOneView.oneview_client import OneViewClient
from functools import partial

import amqp
import amqp.spec
import datetime
import json
import ssl
import datetime


def callback(channel, msg):
Expand Down Expand Up @@ -114,38 +115,34 @@ def recv(host, route):
conn.close()


def login(con, credential):
# Login with givin credentials
try:
con.login(credential)
except:
print('Login failed')


def acceptEULA(con):
def acceptEULA(oneview_client):
# See if we need to accept the EULA before we try to log in
con.get_eula_status()
eula_status = oneview_client.connection.get_eula_status()
try:
if con.get_eula_status() is True:
con.set_eula('no')
if eula_status is True:
oneview_client.connection.set_eula('no')
except Exception as e:
print('EXCEPTION:')
print(e)


def getCertCa(sec):
cert = sec.get_cert_ca()
def getCertCa(oneview_client):
cert = oneview_client.certificate_authority.get()
ca = open('caroot.pem', 'w+')
ca.write(cert)
ca.close()


def genRabbitCa(sec):
sec.gen_rabbitmq_internal_signed_ca()
def genRabbitCa(oneview_client):
certificate_ca_signed_client = {
"commonName": "default",
"type": "RabbitMqClientCertV2"
}
oneview_client.certificate_rabbitmq.generate(certificate_ca_signed_client)


def getRabbitKp(sec):
cert = sec.get_rabbitmq_kp()
def getRabbitKp(oneview_client):
cert = oneview_client.certificate_rabbitmq.get_key_pair('default')
ca = open('client.pem', 'w+')
ca.write(cert['base64SSLCertData'])
ca.close()
Expand Down Expand Up @@ -175,22 +172,25 @@ def main():
action='store_true',
help='Download the required keys and certs then exit')
args = parser.parse_args()
credential = {'userName': args.user, 'password': args.passwd}

con = connection(args.host)
sec = security(con)
config = {
"ip": args.host,
"credentials": {
"userName": args.user,
"password": args.passwd
}
}

login(con, credential)
acceptEULA(con)
oneview_client = OneViewClient(config)
acceptEULA(oneview_client)

# Generate the RabbitMQ keypair (only needs to be done one time)
if args.gen:
genRabbitCa(sec)
genRabbitCa(oneview_client)
sys.exit()

if args.down:
getCertCa(sec)
getRabbitKp(sec)
getCertCa(oneview_client)
getRabbitKp(oneview_client)
sys.exit()

recv(args.host, args.route)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ skip_missing_interpreters = true
[flake8]
ignore = E402
max-line-length = 140
exclude = tests.py, hpOneView/__init__.py, examples/scmb/, examples/scripts
exclude = hpOneView/__init__.py
max-complexity = 14

[testenv]
Expand Down

0 comments on commit 99dbd76

Please sign in to comment.