2

I have below query stored in a variable I got and I need to fetch value of 'resource_status'.

I need 'UPDATE_IN_PROGRESS'

As requested, putting the code here. The variable evntsdata is storing the events list.

try:
    evntsdata = str(hc.events.list(stack_name)[0]).split(" ") # this is the variable that is getting the JSON response (or so) 

    #print(evntsdata[715:733])
    #event_handle = evntsdata[715:733]

    if event_handle == 'UPDATE_IN_PROGRESS':
        loopcontinue = True
        while loopcontinue:
            evntsdata = str(hc.events.list(stack_name)[0]).split(" ")
            #event_handle = evntsdata[715:733]
            if (event_handle == 'UPDATE_COMPLETE'):
                loopcontinue = False
                print(str(timestamp()) + " " + "Stack Update is Completed!" + ' - ' + evntsdata[-3] + ' = ' + evntsdata[-1])
            else:
                print(str(timestamp()) + " " + "Stack Update in Progress!" + ' - ' + evntsdata[-3] + ' = ' + evntsdata[-1])
                time.sleep(10)
    else:
        print("No updates to perform")
        exit(0)

except AttributeError as e:
   print(str(timestamp()) + " " + "ERROR: Stack Update Failure")
   raise

print(evntsdata) has below result

   ['<Event', "{'resource_name':", "'Stackstack1',", "'event_time':", "'2017-05-26T12:10:43',", "'links':", "[{'href':", "'x',", "'rel':", "'self'},", "{'href':", "'x',", "'rel':", "'resource'},", "{'href':", "'x',", "'rel':", "'stack'}],", "'logical_resource_id':", "'Stackstack1',", "'resource_status':", "'UPDATE_IN_PROGRESS',", "'resource_status_reason':", "'Stack", 'UPDATE', "started',", "'physical_resource_id':", "'xxx',", "'id':", "'xxx'}>"]
13
  • 3
    This isn't JSON. Looks like you've printed some Event object. Please show your code with an edit and also include your attempts at parsing this Commented May 26, 2017 at 12:05
  • please check the edit. Let me know if you need any other info Commented May 26, 2017 at 12:13
  • 2
    Whatever hc.events.list(stack_name)[0] is you should not str() and .split() it. This is wrong on so many levels. I assume this Event class has some methods to retrieve the field you are looking for. Use them. Commented May 26, 2017 at 12:15
  • 1
    Yes, the other info would be a minimal reproducible example. Please read carefully and edit again. What is hc, for example? Commented May 26, 2017 at 12:18
  • 1
    @HeenashreeKhandelwal Do not listen to that. This is wrong. This Event object has to have this dictionary inside. You just need a way to retrieve it. Usually you look for something like .data or .content. You seriously want to str it, do shenanigans to remove the envelope and then parse it (which will be hard since this is not JSON)? This is plainly stupid. Read the documentation of this Event class and try to retrieve the data properly. Commented May 26, 2017 at 12:25

2 Answers 2

1

Do not serialize and parse objects when the data is in front of you. This is inefficient and hard to understand and maintain. The solution is quite trivial:

data = hc.events.list(stack_name)[0].to_dict()
event_handle = data['resource_status']
Sign up to request clarification or add additional context in comments.

Comments

0

It's not JSON, it's a class that you've printed

class Event(base.Resource):
    def __repr__(self):
        return "<Event %s>" % self._info

Try poking around the source code to get access to the dictionary self._info, then access your fields according

For example,

event_info = hc.events.list(stack_name)[0]._info
event_handle = event_info['resource_status'] 

Though, there may be another way like calling to_dict() instead, since the underscore indicates a private variable

4 Comments

The only thing I would not do is to use _info (which suggests its a private object). Actually this Event class does have a proper to_dict() method.
I didn't bother looking at the base class
This is so tricky. This github link has that actual code but I really didnt want to go that far. I just want the resource status. github.com/openstack/python-heatclient/blob/master/heatclient/….
Sometimes you need to actually read the documentation (code included) to get your information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.