https://pulumi.com logo
#general
Title
# general
h

helpful-afternoon-10938

11/19/2021, 6:44 PM
Hi, I am working with automation api and need to know if there is any way to get stack diff in a json. like only the resource that has been changed with the change. I can only get dict with two value dict_items([('same', 1), ('update', 1)])
Copy code
diff = stack.preview(diff=True)
print(diff.change_summary)
but I need the changed resource in some json format
r

red-match-15116

11/19/2021, 6:45 PM
The diff is not the
PreviewResult
which is what you're getting from
stack.preview
You might want to do
diff = stack.preview(diff=True, on_output=print)
You can also get more diff information by using the
on_event
callback
stack.preview(on_event=print)
print is just an example here but you can get all of the diff information from the events generated by on_event
You probably want to look at the
ResourcePreEvent
specifically.
h

helpful-afternoon-10938

11/19/2021, 6:48 PM
perfecto, exactly what I was looking for🤘thanks
h

helpful-afternoon-10938

11/19/2021, 7:06 PM
ok got it but what will be the simplest way to get a json output of below data. cause when I'm fetching the stdout or on_output=print I get only string
Copy code
pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:test::BMasServiceEKS::pulumi:pulumi:Stack::BMasServiceEKS-test]
    ~ aws:ec2/securityGroup:SecurityGroup: (update)
        [id=sg-fhc56yrrhc6rh]
        [urn=urn:pulumi:test::BMasServiceEKS::aws:ec2/securityGroup:SecurityGroup::allowTls]
        [provider=urn:pulumi:test::BMasServiceEKS::pulumi:providers:aws::default_4_26_0::htrddhrt-hrthhrd-4e48-9ed2-dhrrtyrddrt]
      ~ ingress: [
          ~ [0]: {
                  ~ cidrBlocks: [
                      - [0]: "0.0.0.0/0"
                    ]
                  - fromPort  : 22
                  - protocol  : "tcp"
                  - toPort    : 22
                }
        ]
r

red-match-15116

11/19/2021, 7:14 PM
You would have to get the information from the events. Using
on_event
. Maybe something like:
stack.preview(on_event=json.dumps)
Or even:
Copy code
def print_resource_pre_as_json(evt: auto.EngineEvent):
    if evt.resource_pre_event:
        json.dumps(evt.resource_pre_event)

stack.preview(on_event=print_resource_pre_as_json)
There's no automatic way to do this so you'll need to experiment
h

helpful-afternoon-10938

11/21/2021, 4:01 PM
indeed had to create a dirty function to get only the diff resources from the string output.