Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added json method to Entity #326

Merged
merged 4 commits into from
Nov 18, 2016
Merged

Added json method to Entity #326

merged 4 commits into from
Nov 18, 2016

Conversation

renzon
Copy link
Contributor

@renzon renzon commented Oct 19, 2016

close #323

Once the methods uses _payload, some Entities maybe need
override this to match names expected on API

I followed @elyezer tip and made a this POC that worked for simple properties: renzon@d092468.

The problem is when the default naming mapping is not correct with the expected for API. But in this case Subclasses can override _to_json_dict for the exceptions.

@ehelms and @rochacbruno could give it a try to check exceptions as they use it and creating issues here for them. What you guys think?

@omaciel
Copy link
Member

omaciel commented Oct 19, 2016

@Ichimonji10 could I ask you for your opinion here?

@coveralls
Copy link

coveralls commented Oct 20, 2016

Coverage Status

Coverage increased (+0.004%) to 98.537% when pulling c4d3dd3 on renzon:323 into 40ee56f on SatelliteQE:master.

@ehelms
Copy link
Contributor

ehelms commented Oct 20, 2016

I think, based on the output below, I will also need json.dumps to work for taking a large data structure containing Nailgun classes and turning them into JSON.

  File "/tmp/ansible_Xv6fUt/ansible_module_katello.py", line 451, in compare
    return json.dumps(reports)
  File "/usr/lib64/python2.7/json/__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib64/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib64/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib64/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: nailgun.entities.Package(nailgun.config.ServerConfig(url='https://sat-r220-02.lab.eng.rdu2.redhat.com/', verify=False, auth=('admin', 'changeme')), nvrea=u'sclo-git25-1.0-2.el7.x86_64', checksum=u'751e639a0b8add0adc0c5cf0bf77693b3197b17533037ce2e7b9daa618898b99', summary=u'Package that installs sclo-git25', filename=u'sclo-git25-1.0-2.el7.x86_64.rpm', epoch=u'0', version=u'1.0', nvra=u'sclo-git25-1.0-2.el7.x86_64', release=u'2.el7', sourcerpm=u'sclo-git25-1.0-2.el7.src.rpm', arch=u'x86_64', id=64529, name=u'sclo-git25') is not JSON serializable

@renzon
Copy link
Contributor Author

renzon commented Oct 26, 2016

@ehelms I suppose you want nested strutures, right? Using default _parse function it just tranform complex structures on their respective id. Check Repository parsed to id. If nested is what you want I can try it, I mean, if you would expect something like:

{ # package regular fields
    {'repository': {'id' : 1}}
}

@renzon
Copy link
Contributor Author

renzon commented Oct 26, 2016

@ehelms I played with the nested structure on last commit, check if it works for you

@coveralls
Copy link

coveralls commented Oct 27, 2016

Coverage Status

Coverage decreased (-0.04%) to 98.502% when pulling 73b0207 on renzon:323 into 272ff30 on SatelliteQE:master.

@coveralls
Copy link

coveralls commented Oct 31, 2016

Coverage Status

Coverage decreased (-0.04%) to 98.508% when pulling 1b4a70f on renzon:323 into 272ff30 on SatelliteQE:master.

@renzon
Copy link
Contributor Author

renzon commented Nov 4, 2016

@ehelms last time we spoke it was working in your tests. Is it still working? Let me know if it is so folk can merge and made a new version available on pypi ;)

@renzon renzon added review and removed in progress labels Nov 4, 2016
Copy link
Contributor

@abalakh abalakh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK pending some small comments

'name': u'sclo-git25'
}
cfg = config.ServerConfig(
url='https://sat-r220-02.lab.eng.rdu2.redhat.com/', verify=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove internal url, we shouldn't use any of such in our public repos

'name': u'sclo-git25'
}
cfg = config.ServerConfig(
url='https://sat-r220-02.lab.eng.rdu2.redhat.com/', verify=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove internal url

@@ -510,6 +511,44 @@ def __repr__(self):
)
)

def to_json(self):
r"""Create a JSON encoded string with Entity properties. Ex:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line needed here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw what does that r""" format stands for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abalakh : this is for Sphinx and Flake do not fail when reading my string. I got Travis errors while generating doc and solved after checking it on SO:

Copy link
Contributor

@elyezer elyezer Nov 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the raw string you could use \\**kwargs, that should work.

>>> org.to_json()
'{"id": 1, "name": "Nailgun Org"}')

:return: str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And another empty line after this one

and build a list calling the same method on each object on OneToMany
fields.

:return: dct
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another empty line needed here:

System Message: WARNING/2 (<string>, line 8)

Field list ends without a blank line; unexpected unindent.

""" Transforms obj into a json serializable object.

:param obj: entity or any json serializable object
:return: serializable object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line is needed after this line:

System Message: WARNING/2 (<string>, line 5)

Field list ends without a blank line; unexpected unindent.

... }
>>> org = entities.Organization(config.ServerConfig('foo'), \*\*kwargs)
>>> org.to_json()
'{"id": 1, "name": "Nailgun Org"}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: closing bracket is probably redundant here

if isinstance(field, OneToOneField):
json_dct[field_name] = values[field_name].to_json_dict()
elif isinstance(field, OneToManyField):
json_dct[field_name] = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not covered by tests

@abalakh
Copy link
Contributor

abalakh commented Nov 7, 2016

Btw please include transformation result for one 'real' entity with numerous fields/subentities.

@coveralls
Copy link

coveralls commented Nov 7, 2016

Coverage Status

Coverage increased (+0.02%) to 98.567% when pulling 1e3e291 on renzon:323 into 272ff30 on SatelliteQE:master.

Copy link
Contributor

@abalakh abalakh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK pending the last nitpick

>>> org.to_json()
'{"id": 1, "name": "Nailgun Org"}'

:return: str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the last one empty line missing after this one

@coveralls
Copy link

coveralls commented Nov 9, 2016

Coverage Status

Coverage increased (+0.02%) to 98.567% when pulling 4204168 on renzon:323 into 272ff30 on SatelliteQE:master.

@abalakh
Copy link
Contributor

abalakh commented Nov 9, 2016

@renzon you misunderstood my suggestion, i was asking about adding a blank line after :return:, not about removing it :)
It's just some rst checkers complain that blank line is needed after it like this:

System Message: WARNING/2 (<string>, line 8)

Field list ends without a blank line; unexpected unindent.

However, I've checked sphinx and it didn't throw any warning, build succeeded and resulting doc had no issues, so i can't see the reason to block this PR anymore, my suggestion is definitely optional.

@rochacbruno
Copy link
Contributor

@renzon can you put some demonstration on Nailgun docs? some examples on how to use and also output examples of json methods.

maybe in http://nailgun.readthedocs.io/en/latest/examples.html (https://github.com/SatelliteQE/nailgun/blob/master/docs/examples.rst)

@coveralls
Copy link

coveralls commented Nov 11, 2016

Coverage Status

Coverage increased (+0.02%) to 98.567% when pulling 3a904be on renzon:323 into 272ff30 on SatelliteQE:master.

@renzon renzon self-assigned this Nov 15, 2016
@rochacbruno
Copy link
Contributor

ACK

@rochacbruno rochacbruno merged commit 2dc93ba into SatelliteQE:master Nov 18, 2016
@renzon renzon deleted the 323 branch December 16, 2016 09:40
elyezer added a commit to elyezer/nailgun that referenced this pull request Mar 6, 2017
Shortlog of commits since last release:

    Andrii Balakhtar (4):
          Fix read() of hostgroup without content_source, lce or cv for 6.1
          Increase timeout for manifest delete/refresh (SatelliteQE#359)
          Provide default name length & type for ComputeResource entities (SatelliteQE#369)
          Implemented Interface entity with all its fields and basic operations (SatelliteQE#373)

    Djebran Lezzoum (1):
          Merge pull request SatelliteQE#380 from svtkachenko/ct_update_payload

    Elyézer Rezende (1):
          Update travis config

    Oleksandr Shtaier (8):
          Merge pull request SatelliteQE#341 from sghai/update-test
          Merge pull request SatelliteQE#342 from renzon/332
          Merge pull request SatelliteQE#344 from elyezer/update-travis
          Merge pull request SatelliteQE#352 from svtkachenko/sv_payload
          Merge pull request SatelliteQE#345 from abalakh/fix_hg_61_workaround
          Merge pull request SatelliteQE#366 from svtkachenko/update_template_kind
          Merge pull request SatelliteQE#361 from svtkachenko/update_sv
          Merge pull request SatelliteQE#383 from svtkachenko/63_image

    Renzo Nuccitelli (3):
          Added json method to Entity (SatelliteQE#326)
          Merge pull request SatelliteQE#347 from svtkachenko/add_image
          Implemented Entity __eq__ method comparing all fields (SatelliteQE#350)

    Stanislav Tkachenko (10):
          Add content related helpers for Repository entity. (SatelliteQE#327)
          Add Create and Delete mixins to Smart Proxy (SatelliteQE#343)
          Add mixins to Image
          Override create_payload and upload_payload for SmartVariable.
          Update Host.puppet_class field name. (SatelliteQE#346)
          Removed overriden read method for Smart Variable
          Update TemplateKind entity
          Add ProvisioningTemplate. (SatelliteQE#365)
          Fix update_payload method for ConfigTemplate/ProvisioningTemplate
          [6.2.z] Update Image and Host entities (SatelliteQE#356)

    oshtaier (2):
          Add UpdateMixin for CV Filter Rules entity
          Add host count field to CV entity

    renzon (5):
          Fixed ConfigTemplate create and update with TemplateCombination
          ListFiled handled on _payload
          api fixed and methods GET and DELETED added for TemplateCombination
          api fixed and methods GET and DELETED added for TemplateCombination
          api fixed and methods GET and DELETED added for TemplateCombination

    sghai (5):
          Updated discovery_rule entity with org & location (SatelliteQE#320)
          Added a new field root_pass to hostgroup entity (SatelliteQE#336)
          Added unit test for root_pass from hostgroup entity
          Merge pull request SatelliteQE#338 from oshtaier/cvf_update_mixin
          Merge pull request SatelliteQE#340 from oshtaier/cv_field
lpramuk pushed a commit to lpramuk/nailgun that referenced this pull request Sep 26, 2023
* Added nested json parsing to Entity

close SatelliteQE#323

* Added to_json_serializable function

close SatelliteQE#323

* Fixed last nitpick on docstrings

* Added docs for to_json_serializable function
lpramuk pushed a commit to lpramuk/nailgun that referenced this pull request Sep 26, 2023
Shortlog of commits since last release:

    Andrii Balakhtar (4):
          Fix read() of hostgroup without content_source, lce or cv for 6.1
          Increase timeout for manifest delete/refresh (SatelliteQE#359)
          Provide default name length & type for ComputeResource entities (SatelliteQE#369)
          Implemented Interface entity with all its fields and basic operations (SatelliteQE#373)

    Djebran Lezzoum (1):
          Merge pull request SatelliteQE#380 from svtkachenko/ct_update_payload

    Elyézer Rezende (1):
          Update travis config

    Oleksandr Shtaier (8):
          Merge pull request SatelliteQE#341 from sghai/update-test
          Merge pull request SatelliteQE#342 from renzon/332
          Merge pull request SatelliteQE#344 from elyezer/update-travis
          Merge pull request SatelliteQE#352 from svtkachenko/sv_payload
          Merge pull request SatelliteQE#345 from abalakh/fix_hg_61_workaround
          Merge pull request SatelliteQE#366 from svtkachenko/update_template_kind
          Merge pull request SatelliteQE#361 from svtkachenko/update_sv
          Merge pull request SatelliteQE#383 from svtkachenko/63_image

    Renzo Nuccitelli (3):
          Added json method to Entity (SatelliteQE#326)
          Merge pull request SatelliteQE#347 from svtkachenko/add_image
          Implemented Entity __eq__ method comparing all fields (SatelliteQE#350)

    Stanislav Tkachenko (10):
          Add content related helpers for Repository entity. (SatelliteQE#327)
          Add Create and Delete mixins to Smart Proxy (SatelliteQE#343)
          Add mixins to Image
          Override create_payload and upload_payload for SmartVariable.
          Update Host.puppet_class field name. (SatelliteQE#346)
          Removed overriden read method for Smart Variable
          Update TemplateKind entity
          Add ProvisioningTemplate. (SatelliteQE#365)
          Fix update_payload method for ConfigTemplate/ProvisioningTemplate
          [6.2.z] Update Image and Host entities (SatelliteQE#356)

    oshtaier (2):
          Add UpdateMixin for CV Filter Rules entity
          Add host count field to CV entity

    renzon (5):
          Fixed ConfigTemplate create and update with TemplateCombination
          ListFiled handled on _payload
          api fixed and methods GET and DELETED added for TemplateCombination
          api fixed and methods GET and DELETED added for TemplateCombination
          api fixed and methods GET and DELETED added for TemplateCombination

    sghai (5):
          Updated discovery_rule entity with org & location (SatelliteQE#320)
          Added a new field root_pass to hostgroup entity (SatelliteQE#336)
          Added unit test for root_pass from hostgroup entity
          Merge pull request SatelliteQE#338 from oshtaier/cvf_update_mixin
          Merge pull request SatelliteQE#340 from oshtaier/cv_field
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make entities JSON Serializable
7 participants