Skip to content

Conversation

@sudiptob2
Copy link
Contributor

@sudiptob2 sudiptob2 commented May 23, 2023

Description

closes #155

Changes Made

  1. implemented client factory folder structure
  2. implemented creation of DNSClient using factory
  3. refactored get_managed_zones and test_dns_policies with DNSClient
  4. updated unit test to call get_managed_zones and test_dns_policies with new method signature.

Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
@sudiptob2 sudiptob2 changed the title fix:sparkles:: implement client factory for DNS client [#155] refactor:sparkles:: implement client factory for DNS client [#155] May 24, 2023
@sudiptob2 sudiptob2 changed the title refactor:sparkles:: implement client factory for DNS client [#155] refactor 🔨 : implement client factory for DNS client [#155] May 24, 2023
sudiptob2 added 2 commits May 24, 2023 22:06
Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
@sudiptob2 sudiptob2 marked this pull request as ready for review May 24, 2023 16:36
credentials)
project_result['managed_zones'] = crawl.get_managed_zones(
project_id,
ClientFactory.get_client('dns').get_service(credentials),

Choose a reason for hiding this comment

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

Eventually I'd like to move each of these resource-specific client creation calls behind a function that takes the resource string (e.g. "dns") and creds, and leverages a dictionary mapping from resource string to service object. Then we can dynamically update the dict as we loop through the scan_config: either set the newly created object or re-use if it's already there for the resource key.

I'm open to doing this now, or revisiting after the we've implemented each of the clients. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, I'm trying to gain a complete understanding of the motivation behind this approach. Is it because calling ClientFactory.get_client('dns').get_service(credentials) multiple times creates redundant objects in memory? If so I can think of an alternative approach also. 💡

For me, it would be more convenient to incorporate the above refactoring when we implement the double-loop approach. At this moment, I'm having difficulty visualizing the overall implementation, and I think it would be easier for me to grasp if I can start by implementing the simpler parts. Once I have a better understanding of those, I believe I will be able to comprehend this approach more easily 🎯

Choose a reason for hiding this comment

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

Yes, it helps us keep only one client in memory for each resource.

That sounds perfectly reasonable, let's revisit the idea later and introduce it as an isolated PR if we decide to do it.

@staticmethod
@abstractmethod
def get_service(credentials: Credentials) -> discovery.Resource:
"""Create a client.

Choose a reason for hiding this comment

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

Nit: let's update the description here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated.

Choose a reason for hiding this comment

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

This should be "Get a discovery resource for a client", not "Create a client", right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, lets make it more verbose.

Copy link

@under-hill under-hill left a comment

Choose a reason for hiding this comment

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

Some spelling / documentation nits, and discussion about resource->client map

@sudiptob2
Copy link
Contributor Author

Thanks a ton! I am extremely sorry that I missed some of the basic docs and spelling. will be addressing your comments one by one as soon as possible.

@sudiptob2 sudiptob2 marked this pull request as draft May 24, 2023 19:29
sudiptob2 added 4 commits May 25, 2023 01:31
Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
@sudiptob2 sudiptob2 requested a review from under-hill May 25, 2023 15:58
@sudiptob2 sudiptob2 marked this pull request as ready for review May 28, 2023 15:16
Signed-off-by: Sudipto Baral <sudiptobaral.me@gmail.com>
Copy link

@under-hill under-hill left a comment

Choose a reason for hiding this comment

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

More comment nits, and request to add another unit test

An object of discovery.Resource
"""

raise NotImplementedError("Child class must implement create_client")

Choose a reason for hiding this comment

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

do you mean "must implement get_service" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed 🙏

@staticmethod
@abstractmethod
def get_service(credentials: Credentials) -> discovery.Resource:
"""Create a client.

Choose a reason for hiding this comment

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

This should be "Get a discovery resource for a client", not "Create a client", right?

credentials)
project_result['managed_zones'] = crawl.get_managed_zones(
project_id,
ClientFactory.get_client('dns').get_service(credentials),

Choose a reason for hiding this comment

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

Yes, it helps us keep only one client in memory for each resource.

That sounds perfectly reasonable, let's revisit the idea later and introduce it as an isolated PR if we decide to do it.

Choose a reason for hiding this comment

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

Let's add a test for calling ClientFactory.get_client() with a gibberish string argument, and ensuring that it logs an error and returns None as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added test. Also organized tests related to client factory in a Class, later on we can add at least one test case for each client factory here.

Args:
project_name: A name of a project to query info about.
credentials: An google.oauth2.credentials.Credentials object.
service: A resource object for interacting with the Compute API.

Choose a reason for hiding this comment

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

This should say "DNS API", not "Compute API" right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, or can we say GCP API?

Args:
project_id: An id of a project to query info about.
credentials: An google.oauth2.credentials.Credentials object.
service: A resource object for interacting with the Compute API.

Choose a reason for hiding this comment

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

This should say "DNS API", not "Compute API" right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@sudiptob2 sudiptob2 requested a review from under-hill May 30, 2023 05:11
@under-hill under-hill merged commit fce767f into google:main May 30, 2023
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.

Subtask: Implement client factory for DNS client.

2 participants