Create an instance from volume in openstach with python-novaclient - python

I am trying to create an instance from a bootable volume in openstack using python-novaclient.
The steps I am taking are following:
Step1: create a volume with an Image "Centos" with 100GB.
Step2: create an instance with the volume that I created in step1.
However, I must be doing something wrong or missing some information that it is not able to complete the task.
Here are my commands in python shell.
import time, getpass
from cinderclient import client
from novaclient.client import Client
project_name = 'project'
region_name = 'region'
keystone_link = 'https://keystone.net:5000/v2.0'
network_zone = "Public"
key_name = 'key_pair'
user = 'user'
pswd = getpass.getpass('Password: ')
# create a connection
cinder = client.Client('1', user, pswd, project_name, keystone_link, region_name = region_name)
# get the volume id that we will attach
print(cinder.volumes.list())
[<Volume: 1d36203e-b90d-458f-99db-8690148b9600>, <Volume: d734f5fc-87f2-41dd-887e-c586bf76d116>]
vol1 = cinder.volumes.list()[1]
vol1.id
block_device_mapping = {'device_name': vol1.id, 'mapping': '/dev/vda'}
### +++++++++++++++++++++++++++++++++++++++++++++++++++++ ###
# now create a connection with nova and create then instance object
nova = Client(2, user, pswd, project_name, keystone_link, region_name = region_name)
# find the image
image = nova.images.find(name="NETO CentOS 6.4 x86_64 v2.2")
# get the flavor
flavor = nova.flavors.find(name="m1.large")
#get the network and attach
network = nova.networks.find(label=network_zone)
nics = [{'net-id': network.id}]
# get the keyname and attach
key_pair = nova.keypairs.get(key_name)
s1 = 'nova-vol-test'
server = nova.servers.create(name = s1, image = image.id, block_device_mapping = block_device_mapping, flavor = flavor.id, nics = nics, key_name = key_pair.name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 902, in create
**boot_kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 554, in _boot
return_raw=return_raw, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/base.py", line 100, in _create
_resp, body = self.api.client.post(url, body=body)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 490, in post
return self._cs_request(url, 'POST', **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 465, in _cs_request
resp, body = self._time_request(url, method, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 439, in _time_request
resp, body = self.request(url, method, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 433, in request
raise exceptions.from_response(resp, body, url, method)
novaclient.exceptions.BadRequest: Block Device Mapping is Invalid: failed to get volume /dev/vda. (HTTP 400) (Request-ID: req-2b9db4e1-f24f-48c6-8660-822741ca52ad)
>>>
I tried to find any documentation so that I can solve this on my own, however, I was not able to.
If anyone has tried this before, I would appreciate there help on this.
Thanks,
Murtaza

I was able to get it to work by using this dictionary:
block_dev_mapping = {'vda':'uuid of the volume you want to use'}
I then called it in the create method like this:
instance = nova.servers.create(name="python-test3", image='', block_device_mapping=block_dev_mapping,
flavor=flavor, key_name="my-keypair", nics=nics)

Related

azure log analytics InsufficientAccessError

I am trying to read log analytics in python.
Here is my code:
AZURE_CLIENT_ID = ''
AZURE_CLIENT_SECRET = ''
AZURE_TENANT_ID = ''
workspace_id = ''
from azure.identity import ClientSecretCredential
from datetime import datetime
from azure.monitor.query import LogsQueryClient, LogsQueryStatus
start_time = datetime(2022, 1, 1)
end_time = datetime(2023, 1, 2)
credential = ClientSecretCredential(
client_id = AZURE_CLIENT_ID,
client_secret = AZURE_CLIENT_SECRET,
tenant_id = AZURE_TENANT_ID
)
client = LogsQueryClient(credential)
query = "ContainerLog"
response = client.query_workspace(workspace_id=workspace_id,
query=query, timespan=(start_time, end_time - start_time))
if response.status == LogsQueryStatus.PARTIAL:
error = response.partial_error
print('Results are partial', error.message)
elif response.status == LogsQueryStatus.SUCCESS:
results = []
for table in response.tables:
for row in table.rows:
results.append(dict(zip(table.columns, row)))
print(convert_azure_table_to_dict(results))
and it is failing:
Traceback (most recent call last):
File "c:\temp\x.py", line 24, in <module>
response = client.query_workspace(workspace_id=workspace_id,
File "C:\kourosh\venv\lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
return func(*args, **kwargs)
File "C:\kourosh\venv\lib\site-packages\azure\monitor\query\_logs_query_client.py", line 136, in query_workspace
process_error(err, LogsQueryError)
File "C:\kourosh\venv\lib\site-packages\azure\monitor\query\_helpers.py", line 141, in process_error
raise HttpResponseError(message=error.message, response=error.response, model=model)
azure.core.exceptions.HttpResponseError: (InsufficientAccessError) The provided credentials have insufficient access to perform the requested operation
Code: InsufficientAccessError
Message: The provided credentials have insufficient access to perform the requested operation
I have added Log Analytics API -> Data.Read permission to the registered app that I'm using.
Any idea what is causing this?
Data.Read provides permissions to use the API and grant your app access to your Log Analytics Workspace. However, for accessing data within log analytics workspace, you need to provide permissions as per the data you need access to.
References:
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/api/access-api
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/manage-access?tabs=portal

AttributeError: 'Accounts' object has no attribute 'deploy'

The .deploy() function is working for FundMe.sol contract but not for MockV3Interface.sol Contract.
Here's my deploy.py code below:
from brownie import FundMe, MockV3Aggregator, accounts, config, network
from scripts.helpful_scripts import get_account
def deploy_fund_me():
account = get_account()
# Passing Price Feed to our Solidity contract.
# If we are on a persistent network like rinkeby, use its price feed address.
# Otherwise use Mocks.
# print(account)
if network.show_active() != "development":
price_feed_address = config["networks"][network.show_active()][
"eth_usd_price_feed"
]
else:
print(f"The current Network is: {network.show_active()}")
print("Deploying Mock....")
mock_aggregator = MockV3Aggregator.deploy(
18, 2000000000000000000, {"from": accounts}
)
price_feed_address = mock_aggregator.address
print("Mock Deployed!!")
fund_me = FundMe.deploy(
price_feed_address,
{"from": account},
publish_source=True,
)
print(f"It is deployed to {fund_me.address}")
def main():
deploy_fund_me()
And Here's the error Window::
Running '\Users\HP\Development\demos\brownie_fund_me\scripts\deploy.py::main'...
The current Network is: development
Deploying Mock....
File "c:\users\hp\development\demos\brownie_simple_storage\venv\lib\site-packages\brownie\_cli\run.py", line 50, in main
return_value, frame = run(
File "c:\users\hp\development\demos\brownie_simple_storage\venv\lib\site-packages\brownie\project\scripts.py", line 103, in run
return_value = f_locals[method_name](*args, **kwargs)
File "\Users\HP\Development\demos\brownie_fund_me\scripts\deploy.py", line 34, in main
deploy_fund_me()
File "\Users\HP\Development\demos\brownie_fund_me\scripts\deploy.py", line 19, in deploy_fund_me
mock_aggregator = MockV3Aggregator.deploy(
File "c:\users\hp\development\demos\brownie_simple_storage\venv\lib\site-packages\brownie\network\contract.py", line 528, in __call__
return tx["from"].deploy(
AttributeError: 'Accounts' object has no attribute 'deploy'
Terminating local RPC client...
Everyone's Help would be appreciated.
THANKS IN ADVANCE
This is a solution to your problem...use account instead of accounts
from brownie import accounts, config, MyContract
def deploy_my_contract():
account = accounts[0]
my_contract = MyContract.deploy({"from": account})
some_return_value = my_contract.some_func()
print(some_return_value)
I'm doing the same course..
My problem was that accounts[0] was in a function and not returning anything due to missing () in the call
This is now working for me.
def get_account():
if network.show_active == "development":
return accounts[0]
else:
return accounts.add(config["wallets"]["from_key"])
def deploy_fund_me():
account = get_account()
fund_me = FundMe.deploy({"from": account})

f-string in Python with the atlassian-python package. Gives bad request

I'm trying to fetch all issues in JIRA for all projects. When doing the call one at a time, it works perfect. When trying to run it in a for loop I'm prompted with a 400 Client error.
The way that works:
results = jira_instance.jql("project = FJA", limit = 100, fields=["issuetype", "status", "summary"])
The way that does not work:
projects = ["ADV", "WS", "FJA", "FOIJ", "QW", "UOI"]
for key in projects:
results = jira_instance.jql(f"project = {key})", limit = 100, fields=["issuetype", "status", "summary"])
The error:
Traceback (most recent call last):
File "C:\jira-api-python\jira.py", line 24, in <module>
results = jira_instance.jql("project = {key}", limit = 100, fields=["issuetype", "status", "summary"])
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\jira.py", line 2271, in jql
return self.get("rest/api/2/search", params=params)
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\rest_client.py", line 264, in get
response = self.request(
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\rest_client.py", line 236, in request
response.raise_for_status()
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\requests\models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://stuff.atlassian.net/rest/api/2/search?startAt=0&maxResults=100&fields=issuetype%2Cstatus%2Csummary&jql=project+%3D+%7Bkey%7D
My guess is that I'm not using the f-string correct. But when I print the value of {key} it is correct.
Any pointers would be greatly appreciated.
Thank you for your time.
Edit:
Added the full traceback, only removed the path to my machine and changed the URL to the endpoint. Below is the full file with credentials and endpoint redacted. The ideas is to create a csv for each project.
The full code:
from atlassian import Jira
import pandas as pd
import time
jira_instance = Jira(
url = "https://stuff.atlassian.net/",
username = "user",
password = "pass",
)
projects = ["ADV", "WS", "FJA", "FOIJ", "QW", "UOI"]
FIELDS_OF_INTEREST = ["key", "fields.summary", "fields.status.name"]
timestamp = time.strftime("%Y%m%d-%H%M%S")
file_ending = ".csv"
for key in projects:
print(f"stuff = {key}")
results = jira_instance.jql(f"project = {key})", limit = 1000, fields=["issuetype", "status", "summary"])
I found the very simple solution.
In this snippet: results = jira_instance.jql(f"project = {key})", limit = 1000, fields=["issuetype", "status", "summary"])
The ) after {key} was not supposed to be there.
Thank you for the help

Python with sqlalchemy db retrival

I am quite new to python and to sqlalchemy.I have written the following netowrk program.
class SourcetoPort(Base):
""""""
__tablename__ = 'source_to_port'
id = Column(Integer, primary_key=True)
port_no = Column(Integer)
src_address = Column(String)
#----------------------------------------------------------------------
def __init__(self, src_address,port_no):
""""""
self.src_address = src_address
self.port_no = port_no
#
def act_like_switch (self, packet, packet_in):
"""
Implement switch-like behavior.
"""
# Learn the port for the source MAC
print "RECIEVED FROM PORT ",packet_in.in_port , "SOURCE ",packet.src
Session = sessionmaker(bind=engine)
session = Session()
self.mac_to_port[packet.src]=packet_in.in_port
if(self.matrix.get((packet.src,packet.dst))==None):
# create a Session
#print "creating a db session"
#Session = sessionmaker(bind=engine)
#session = Session()
self.matrix[(packet.src,packet.dst)]=0
# Create an entry with address and port
print "creating a new db entry"
entry = SourcetoPort(src_address="packet.src" , port_no=packet_in.in_port)
# Add the record to the session object
session.add(entry)
session.commit()
self.matrix[(packet.src,packet.dst)]+=1
#if self.mac_to_port.get(packet.dst)!=None:
if session.query(SourcetoPort).filter_by(src_address=packet.dst).all():
#send this packet
self.send_packet(packet_in.buffer_id, packet_in.data,self.mac_to_port[packet.dst], packet_in.in_port)
#create a flow modification message
msg = of.ofp_flow_mod()
#set the fields to match from the incoming packet
msg.match = of.ofp_match.from_packet(packet)
#print "SENDING TO PORT " + str(self.mac_to_port[packet.dst]), packet.dst
# send the rule to the switch so that it does not query the controller again.
msg.actions.append(of.ofp_action_output(port=self.mac_to_port[packet.dst]))
# push the rule
self.connection.send(msg)
else:
#print 'flooding the packet '
# Flood this packet out as we don't know about this node.
self.send_packet(packet_in.buffer_id, packet_in.data,
of.OFPP_FLOOD, packet_in.in_port)
In the above code the line
if session.query(SourcetoPort).filter_by(src_address='packet.dst').all():
does not work as I expect it.What I expect is that it should retrive the entry from sqlalchemy database and if it succeeds (as the output is not NONE) it should execute the following code.
When I try to print that line using print
print "session query", session.query(SourcetoPort).filter_by(src_address='packet.dst').all()
The output that I get is
session query []
Am I doing something wrong.It would be great if someone could point this out.
If I change the above line based on the suggestion as
print session.query(SourcetoPort).filter_by(src_address=packet.dst).count()
I get the following error:
creating a new db entry
RECIEVED FROM PORT 2 SOURCE 96:74:ba:a9:92:b9
creating a new db entry
ERROR:core:Exception while handling Connection!PacketIn...
Traceback (most recent call last):
File "ws_thesis/pox/pox/lib/revent/revent.py", line 234, in raiseEventNoErrors
return self.raiseEvent(event, *args, **kw)
File "ws_thesis/pox/pox/lib/revent/revent.py", line 281, in raiseEvent
rv = event._invoke(handler, *args, **kw)
File "ws_thesis/pox/pox/lib/revent/revent.py", line 159, in _invoke
return handler(self, *args, **kw)
File "ws_thesis/pox/tutorial.py", line 137, in _handle_PacketIn
self.act_like_switch(packet, packet_in)
File "ws_thesis/pox/tutorial.py", line 101, in act_like_switch
print session.query(SourcetoPort).filter_by(src_address=packet.dst).count()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2400, in count
return self.from_self(col).scalar()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2045, in scalar
ret = self.one()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2014, in one
ret = list(self)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2057, in __iter__
return self._execute_and_instances(context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2072, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1405, in execute
If you want to filter items by the contents of a variable, you need to use that variable directly:
session.query(SourcetoPort).filter_by(src_address=packet.dst)
You were instead trying to filter by the string 'packet.dst'.
You made the same mistake when creating the SourcetoPort entry; you probably wanted to store result of the packet.src expression, not the string 'packet.src':
SourcetoPort(src_address=packet.src, port_no=packet_in.in_port)
Note that calling .all() retrieves all matching entries from the database, but you are only using it in a if test. It would be more efficient (potentially much more so) to use .count() instead of .all() to let the database tell us how many items match:
if session.query(SourcetoPort).filter_by(src_address=packet.dst).count():

How to store RSA encrypted data to postgresql by using pycrypto?

I want to use Public/Private key to secure my UserInfo data. I'm new with PyCrypto and PostgreSQL.
I have some items to clarify:
Are Public Key and Private Key constant values?
If it is constant, how can I store it properly?
Lastly but the most important, how can I store my encrypted data to PostgreSQL? and retrieve it for verification?
Would you guide me on how to dealt with Crypto.PublicKey.RSA as method to secure my data.
Environment: Python 2.5, PyCrypto 2.3, PostgreSQL 8.3 UTF-8 encoding
UserInfo model:
class UserInfo(models.Model):
userid = models.TextField(primary_key = True)
password = models.TextField(null = True)
keyword = models.TextField(null = True)
key = models.TextField(null = True, blank = True)
date = models.DateTimeField(null = True, blank = True)
UPDATES1
tests.py:
# -*- encoding:utf-8 -*-
import os
from os.path import abspath, dirname
import sys
from py23.service.models import UserInfo
from Crypto import Random
# Set up django
project_dir = abspath(dirname(dirname(__file__)))
sys.path.insert(0, project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'py23.settings'
from django.test.testcases import TestCase
class AuthenticationTestCase(TestCase):
def test_001_registerUserInfo(self):
import Crypto.PublicKey.RSA
import Crypto.Util.randpool
#pool = Crypto.Util.randpool.RandomPool()
rng = Random.new().read
# craete RSA object by random key
# 1024bit
#rsa = Crypto.PublicKey.RSA.generate(1024, pool.get_bytes)
rsa = Crypto.PublicKey.RSA.generate(1024, rng)
# retrieve public key
pub_rsa = rsa.publickey()
# create RSA object by tuple
# rsa.n is public key?, rsa.d is private key?
priv_rsa = Crypto.PublicKey.RSA.construct((rsa.n, rsa.e, rsa.d))
# encryption
enc = pub_rsa.encrypt("hello", "")
# decryption
dec = priv_rsa.decrypt(enc)
print "private: n=%d, e=%d, d=%d, p=%d, q=%d, u=%d" % (rsa.n, rsa.e, rsa.d, rsa.p, rsa.q, rsa.u)
print "public: n=%d, e=%d" % (pub_rsa.n, pub_rsa.e)
print "encrypt:", enc
print "decrypt:", dec
# text to be signed
text = "hello"
signature = priv_rsa.sign(text, "")
# check if the text has not changed
print pub_rsa.verify(text, signature)
print pub_rsa.verify(text+"a", signature)
# userid = models.TextField(primary_key = True)
# password = models.TextField(null = True)
# keyword = models.TextField(null = True)
# key = models.TextField(null = True, blank = True) is it correct to store the public key here?
# date = models.DateTimeField(null = True, blank = True)
userInfo = UserInfo(userid='test1', password=enc[0], key=pub_rsa.n)
userInfo.save()
print "ok"
result here (failed):
======================================================================
ERROR: test_001_registerUserInfo (py23.service.auth.tests.AuthenticationTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\PIDevelopment\workspace37_pydev\pyh23\py23\service\auth\tests.py", line 64, in test_001_registerUserInfo
userInfo.save()
File "C:\Python25\lib\site-packages\django\db\models\base.py", line 458, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "C:\Python25\lib\site-packages\django\db\models\base.py", line 551, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "C:\Python25\Lib\site-packages\django\db\models\manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "C:\Python25\lib\site-packages\django\db\models\query.py", line 1524, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python25\lib\site-packages\django\db\models\sql\compiler.py", line 788, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "C:\Python25\lib\site-packages\django\db\models\sql\compiler.py", line 732, in execute_sql
cursor.execute(sql, params)
File "C:\Python25\lib\site-packages\django\db\backends\util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "C:\Python25\lib\site-packages\django\db\backends\postgresql_psycopg2\base.py", line 44, in execute
return self.cursor.execute(query, args)
DatabaseError: invalid byte sequence for encoding "UTF8": 0x97
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
----------------------------------------------------------------------
Ran 1 test in 90.047s
FAILED (errors=1)
Your problem is that you are trying to store binary data in a text file. Try armoring the data or use bytea (with proper encoding/decoding).

Categories