I am working on creating custom image in IBM Cloud using python. I have a very simple straight code for just creating the image and it fails.
As per me I am passing the relevant correct details for all the parameters.
Still I get an Error which is not much descriptive :
ERROR:root:Please check whether the resource you are requesting exists.
Traceback (most recent call last):
File "/Users/deepali.mittal/GITHUB/dcoa/python/build/dmittal/virtual-env36/lib/python3.6/site-packages/ibm_cloud_sdk_core/base_service.py", line 246, in send
response.status_code, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: Please check whether the resource you are requesting exists., Code: 400
Process finished with exit code 0
This is not related to the resource missing in COS. As if it is not able to find the image in COS it gives a different error.
Code :
from ibm_vpc import VpcV1 as vpc_client
from ibm_cloud_sdk_core import ApiException
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from boto3 import client as boto3_client
import logging
#logging.basicConfig(level=logging.DEBUG)
SOURCE_OBJECT_PATH = 'cos://us-south/de-images-dmittal/abc.qcow2'
RESOURCE_GROUP_ID = '1234'
OPERATING_SYSTEM = 'ubuntu-16-amd64'
def create_ssm_client():
ssm_client = boto3_client("ssm", region_name="us-west-2")
return ssm_client
def retrieve_ibm_config(ssm_client):
params = ["/ibm/service-key"]
response = ssm_client.get_parameters(Names=params, WithDecryption=True)
try:
api_key = response["Parameters"][0]["Value"]
except (ValueError, IndexError):
raise RuntimeError(
f"Required SSM parameters not retrieved. "
f'Required parameters are: {params}.'
)
return api_key
def create_authenticator(api_key):
authenticator = IAMAuthenticator(api_key)
return authenticator
def create_ibm_client(authenticator):
ibm_client = vpc_client('2021-05-28', authenticator=authenticator)
return ibm_client
def create_image_prototype():
image_file_prototype_model = {'href': SOURCE_OBJECT_PATH}
operating_system_identity_model = {'name': OPERATING_SYSTEM}
resource_group_identity_model = {'id': RESOURCE_GROUP_ID}
image_prototype_model = {
'name': 'my-image',
#'resource_group': resource_group_identity_model,
'file': image_file_prototype_model,
'operating_system': operating_system_identity_model
}
image_prototype = image_prototype_model
return image_prototype
def create_image():
ssm_client = create_ssm_client()
api_key = retrieve_ibm_config(ssm_client)
authenticator = create_authenticator(api_key)
ibm_client = create_ibm_client(authenticator)
image_prototype = create_image_prototype()
try:
#images = ibm_client.list_images()
#print(vpc)
#ibm_client.set_service_url('https://us-south.iaas.cloud.ibm.com/v1')
response = ibm_client.create_image(image_prototype)
print(response)
except ApiException as e:
print("Failed")
if __name__ == "__main__":
create_image()
Issue was with IAM Permission. After fixing it worked, the error shown was not relevant so it took time to figure out
Related
I'm using aws lambda for a slack app, and I'm handling an interactive response(so I need to send a response in 3 seconds)
I invoke another lambda in my code with the Event type, and returning a return {"statusCode": 200} but I can't find in cw logs the returned value, the lambda execute with no issues but there is no returned value.
this is my code:
import logging
from urllib.parse import parse_qs
import utils.slack.client as slack
from functions.flows.update_zendesk_ticket import pass_to_pso
from lambda_warmer.lambda_warmer import lambda_warmup
from utils.common import invoke_lambda, PSO_NOC_ALERTS
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
#lambda_warmup()
def lambda_handler(event, context):
logger.info(f'this is the event: {event}')
logger.info(f'this is the context: {context}')
params = dict(parse_qs(event['body'], keep_blank_values=False))
if "payload" in params:
payload = eval(params["payload"][0].replace('false', 'False').replace('null', 'None').replace('true', 'True'))
if payload["type"] == "message_action":
logger.info(f'{payload["user"]["username"]} clicked on {payload["view"]["callback_id"]}')
elif payload["type"] == "view_submission":
logger.debug(payload)
logger.info(f'{payload["user"]["username"]} submitted {payload["view"]["callback_id"]}')
submitted_data = payload["view"]["state"]["values"]
logger.info(submitted_data)
if payload["view"]["callback_id"] == "pass_to_pso":
result = pass_to_pso_handler(submitted_data)
return result
return {"statusCode": 200}
def pass_to_pso_handler(submitted_data):
pso_slack_id = submitted_data["pso"]["pso_select-action"]["selected_user"]
slack_client = slack.SlackClient()
pso_email = slack_client.get_email_from_slack(pso_slack_id)
zd_ticket_id = submitted_data["ticket_id"]["ticket_id-action"]["value"]
thread_link = submitted_data["thread_link"]["thread_link-action"]["value"]
reply_language = submitted_data["reply_language"]["reply_language-action"]["selected_option"][
"value"]
reply_type = submitted_data["reply_type"]["reply_type-action"]["selected_option"]["value"]
pass_to_pso(pso_email=pso_email, ticket_id=zd_ticket_id, thread_link=thread_link,
reply_language=reply_language, reply_type=reply_type)
pso_name = pso_email.split('#')[0]
invoke_lambda({
"pso": pso_name,
"ticket id": zd_ticket_id,
"channel_id": PSO_NOC_ALERTS
}, "Event")
return {"statusCode": 200}
the invoke function:
def invoke_lambda(payload, invocation_type):
client = boto3.client('lambda', 'us-east-1')
response = client.invoke(
FunctionName=SLACK_MESSAGE_LAMBDA,
InvocationType=invocation_type,
Payload=bytes(json.dumps(payload), encoding='utf8'))
and this is the last rows of my cw logs
I think the only way to log your return to CloudWatch Logs by printing it. Or else, it is only visible to your function's integrations such as API Gateway.
import json
status = {"statusCode": 200}
print(json.dumps(status))
return status
I am trying to read and pass the response of work requests in OCI for my compartment.
import oci
import configparser
import json
from oci.work_requests import WorkRequestClient
DEFAULT_CONFIG = "~/.oci/config"
DEFAULT_PROFILE = "DEFAULT"
config_file="config.json"
ab=[]
def config_file_parser(config_file):
config=configparser.ConfigParser()
config.read(config_file)
profile=config.sections()
for config_profile in profile:
func1 = get_work_request(file=config_file, profile_name=config_profile)
get_print_details(func1)
def get_work_request(file=DEFAULT_CONFIG, profile_name=DEFAULT_PROFILE):
global oci_config, identity_client, work_request_client
oci_config = oci.config.from_file(file, profile_name=profile_name)
identity_client = oci.identity.identity_client.IdentityClient(oci_config)
core_client = oci.core.ComputeClient(oci_config)
work_request_client = WorkRequestClient(oci_config)
work_requests = work_request_client.list_work_requests(oci_config["compartment"]).data
print("{} Work Requests found.".format(len(work_requests)))
return work_requests
def get_print_details(workrequest_id):
resp = work_request_client.get_work_request(','.join([str(i["id"]) for i in workrequest_id]))
wrDetails = resp.data
print()
print()
print('=' * 90)
print('Work Request Details: {}'.format(workrequest_id))
print('=' * 90)
print("{}".format(wrDetails))
print()
if __name__ == "__main__":
config_file_parser(config_file)
But while executing work_request_client.get_work_request I am getting TypeError: 'WorkRequestSummary' object is not subscriptable I have tried multiple times with making as object JSON but still the error remains, any way to solve or any leads would be great.
I don't think get_work_request supports passing in multiple work request ids. You'd need to call get_work_request individually for each work request id.
I try to use Google Calendar API
events_result = service.events().list(calendarId=calendarId,
timeMax=now,
alwaysIncludeEmail=True,
maxResults=100, singleEvents=True,
orderBy='startTime').execute()
Everything is ok, when I have permission to access the calendarId, but it will be errors if wrong when I don't have calendarId permission.
I build an autoload.py function with schedule python to load events every 10 mins, this function will be stopped if error come, and I have to use SSH terminal to restart autoload.py manually
So i want to know:
How can I get status_code, example, if it is 404, python will PASS
Answer:
You can use a try/except block within a loop to go through all your calendars, and skip over accesses which throw an error.
Code Example:
To get the error code, make sure to import json:
import json
and then you can get the error code out of the Exception:
calendarIds = ["calendar ID 1", "calendar ID 2", "calendar Id 3", "etc"]
for i in calendarIds:
try:
events_result = service.events().list(calendarId=i,
timeMax=now,
alwaysIncludeEmail=True,
maxResults=100, singleEvents=True,
orderBy='startTime').execute()
except Exception as e:
print(json.loads(e.content)['error']['code'])
continue
Further Reading:
Python Try Except - w3schools
Python For Loops - w3schools
Thanks to #Rafa Guillermo, I uploaded the full code to the autoload.py program, but I also wanted to know, how to get response json or status_code for request Google API.
The solution:
try:
code here
except Exception as e:
continue
import schedule
import time
from datetime import datetime
import dir
import sqlite3
from project.function import cmsCalendar as cal
db_file = str(dir.dir) + '/admin.sqlite'
def get_list_shop_from_db(db_file):
cur = sqlite3.connect(db_file).cursor()
query = cur.execute('SELECT * FROM Shop')
colname = [ d[0] for d in query.description ]
result_list = [ dict(zip(colname, r)) for r in query.fetchall() ]
cur.close()
cur.connection.close()
return result_list
def auto_load_google_database(list_shop, calendarError=False):
shopId = 0
for shop in list_shop:
try:
shopId = shopId+1
print("dang ghi vao shop", shopId)
service = cal.service_build()
shop_step_time_db = list_shop[shopId]['shop_step_time']
shop_duration_db = list_shop[shopId]['shop_duration']
slot_available = list_shop[shopId]['shop_slots']
slot_available = int(slot_available)
workers = list_shop[shopId]['shop_workers']
workers = int(workers)
calendarId = list_shop[shopId]['shop_calendarId']
if slot_available > workers:
a = workers
else:
a = slot_available
if shop_duration_db == None:
shop_duration_db = '30'
if shop_step_time_db == None:
shop_step_time_db = '15'
shop_duration = int(shop_duration_db)
shop_step_time = int(shop_step_time_db)
shop_start_time = list_shop[shopId]['shop_start_time']
shop_start_time = datetime.strptime(shop_start_time, "%H:%M:%S.%f").time()
shop_end_time = list_shop[shopId]['shop_end_time']
shop_end_time = datetime.strptime(shop_end_time, "%H:%M:%S.%f").time()
# nang luc moi khung gio lay ra tu file Json WorkShop.js
booking_status = cal.auto_load_listtimes(service, shopId, calendarId, shop_step_time, shop_duration, a,
shop_start_time,
shop_end_time)
except Exception as e:
continue
def main():
list_shop = get_list_shop_from_db(db_file)
auto_load_google_database(list_shop)
if __name__ == '__main__':
main()
schedule.every(5).minutes.do(main)
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)
I am making telegram bot using python 3 on RPI and for HTTP requesting I used requests library
I wrote the code that should answer &start command:
import requests as rq
updateURL="https://api.telegram.org/bot925438333:AAGEr3pf3c4Fz91sL79mwJ6aGYm-Y6BM7_4/getUpdates"
while True:
r=rq.post(url = updateURL)
data = r.json()
messageArray = data['result']
lastMsgID=len(messageArray)-1
lastMsgData = messageArray[lastMsgID]
lastMsgSenderID = lastMsgData['message']['from']['id']
lastMsgUsername = lastMsgData['message']['from']['username']
lastMsgText = lastMsgData["message"]["text"]
lastMsgChatType = lastMsgData['message']['chat']['type']
if lastMsgChatType == "group":
lastMsgGroupID = lastMsgData['message']['chat']['id']
if lastMsgText == "&start":
if lastMsgChatType == "private":
URL="https://api.telegram.org/bot925438333:AAGEr3pf3c4Fz91sL79mwJ6aGYm-Y6BM7_4/sendMessage"
chatText="Witamy w KozelBot"
chatID=lastMsgSenderID
Params={"chat_id":chatID,"text":chatText}
rs = rq.get(url = URL, params = Params)
if lastMsgChatType == "group":
URL="https://api.telegram.org/bot925438333:AAGEr3pf3c4Fz91sL79mwJ6aGYm-Y6BM7_4/sendMessage"
chatText="Witamy w KozelBot"
chatID=lastMsgGroupID
Params={"chat_id":chatID,"text":chatText}
rs = rq.get(url = URL, params = Params)
but the code outputs an error:
File "/home/pi/telegramResponse.py", line 16, in
lastMsgText = lastMsgData["message"]["text"]
KeyError: 'text'
I don't know how to solve this problem because this fragment is working fine in my other scripts!
Please help!
The reason was simple!
Last message that program finds doesn't contain any text because it was new user notification. KeyError just occured because last message doesn't have ['text'] parameter.
I am working on a woocommerce rest api in which I am passing the product data from mongodb using python. I wrote a script for the same. First time the script ran successfully and passed two products but when I tried to pass more products it is failing. I don't know where I am doing it wrong.
Showing this error:
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='localhost', port=80): Read timed out. (read timeout=5)
script:
from woocommerce import API
import os, sys
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_dir)
from portal_v2.settings import _MONGO_DB_URI
from pymongo import MongoClient
import os, csv, ast
wcapi = API(
url="http://localhost/wordpress1",
consumer_key="ck_******************************************",
consumer_secret="cs_******************************************",
wp_api=True,
version="wc/v1"
)
class Products(object):
def __init__(self, dbname):
_client = MongoClient(_MONGO_DB_URI)
self._database = _client[dbname]
self.getCollections()
self.dbname = dbname
def getCollections(self):
self.products = self._database['products']
def getProducts(self):
product_dict = []
for each in self.products.find().limit(10):
product_dict.append({"name": each['name'],"slug": each['name'].replace(' ','').replace('/','') + each['sku'].replace(' ','').replace('/',''),"type": "simple","status": "publish","featured": False,"catalog_visibility": "visible","description": each['description'],"sku": each['sku'],"regular_price": each['mrp'],"sale_price": each['cost_price'],"date_on_sale_from": "","date_on_sale_to": "","purchasable": True,"total_sales": 0,"virtual": False,"downloadable": False,"downloads": [],"download_limit": -1,"download_expiry": -1,"download_type": "standard","external_url": "","button_text": "","tax_status": "taxable","tax_class": "","manage_stock": False,"stock_quantity": None,"in_stock": True,"backorders": "no","backorders_allowed": False,"backordered": False, "sold_individually": False, "weight": each['weight_gms'],"dimensions": {"length": each['length_mm'],"width": each['width_mm'],"height": each['height_mm']},"shipping_required": True,"shipping_taxable": True,"shipping_class": "", "shipping_class_id": 0,"reviews_allowed": True,"average_rating": "0.00","rating_count": 0,"related_ids": [],"upsell_ids": [],"cross_sell_ids": [],"parent_id": 0,"purchase_note": "","categories": [{"id": 9,},{"id": 14,}],"tags": [],"images": [{"src": each['image_url'].replace('dl=0','raw=1'),"name": each['name'],"position": 0}],"attributes": [],"default_attributes": [],"variations": [],"menu_order": 0})
data = {'create':product_dict}
print data
return data
def updateProducts(self, data):
wcapi.post("products/batch", data)
# print wcapi.get("products/?per_page=45").json()
data = Products('xyz').getProducts()
Products('xyz').updateProducts(data)
where 'xyz' is the database name.
Just increase the timeout option. something like:
wcapi = API(
url="http://localhost/wordpress1",
consumer_key="ck_******************************************",
consumer_secret="cs_******************************************",
wp_api=True,
version="wc/v1",
timeout=10 # the default is 5, increase to whatever works for you.
)