Anyone figure out a neat design pattern to enable ...
# cloudengineering-support
n
Anyone figure out a neat design pattern to enable GCP service api's needed per project? Like an idempotent operation to skip and proceed if already enabled.
g
I made named singletons by the project id and service api
On every resource I add all the apis required as dependencies, those will be created only once and as soon as a service is not depended by any resource the service will be removed
n
I didn't quite grasp the last part, you call
projectService
for each required service. When it is no longer required, do you mean it will no longer be called or it will disable that api too?
g
The Pulumi resource of that API will be gone
If you configured it to disable the api once the resource is deleted then yes, the api will be disabled
It is the
disableOnDestroy
option
I set it to false to prevent breaking things. None of my resources might be using that API, but some of the google services might depend on them indirectly, like Cloud Scheduler, that depends on AppEngine, that depends on Cloud Storage
n
It works but I am hesitant to bring singleton pattern to JS. I just ask myself if it would be better to have code that checks before enabling the api instead of a singleton pattern.
g
What you need is the behaviour. If you have a global map of projectIds pointing to maps of services you could just return the service if it is there.
The singleton method that I went with is waay overengineered for this use case. I used it because I already had the singleton class being used in some other resources, so it made sense for the context
n
Yeah that makes sense