modules/mu/clouds/google.rb in cloud-mu-2.0.0.pre.alpha3 vs modules/mu/clouds/google.rb in cloud-mu-2.0.0.pre.alpha4

- old
+ new

@@ -240,10 +240,14 @@ # Determine whether we (the Mu master, presumably) are hosted in this # cloud. # @return [Boolean] def self.hosted? + if $MU_CFG.has_key?("google_is_hosted") + @@is_in_aws = $MU_CFG["google_is_hosted"] + return $MU_CFG["google_is_hosted"] + end if !@@is_in_gcp.nil? return @@is_in_gcp end if getGoogleMetaData("instance/name") @@ -409,17 +413,34 @@ end # Our credentials map to a project, an organizational structure in Google # Cloud. This fetches the identifier of the project associated with our # default credentials. + # @param credentials [String] + # @return [String] def self.defaultProject(credentials = nil) cfg = credConfig(credentials) return myProject if !cfg or !cfg['project'] loadCredentials(credentials) if !@@authorizers[credentials] cfg['project'] end + # We want a default place to put new projects for the Habitat resource, + # so if we have a root folder, we can go ahead and use that. + # @param credentials [String] + # @return [String] + def self.defaultFolder(credentials = nil) + project = defaultProject(credentials) + resp = MU::Cloud::Google.resource_manager(credentials: credentials).get_project_ancestry(project) + resp.ancestor.each { |a| + if a.resource_id.type == "folder" + return a.resource_id.id + end + } + nil + end + # List all Google Cloud Platform projects available to our credentials def self.listProjects(credentials = nil) cfg = credConfig(credentials) return [] if !cfg or !cfg['project'] result = MU::Cloud::Google.resource_manager.list_projects @@ -573,10 +594,23 @@ elsif subclass.is_a?(Symbol) return Object.const_get("::Google").const_get("Apis").const_get("CloudresourcemanagerV1").const_get(subclass) end end + # Google's Cloud Resource Manager API V2, which apparently has all the folder bits + # @param subclass [<Google::Apis::CloudresourcemanagerV2beta1>]: If specified, will return the class ::Google::Apis::CloudresourcemanagerV2beta1::subclass instead of an API client instance + def self.folder(subclass = nil, credentials: nil) + require 'google/apis/cloudresourcemanager_v2beta1' + + if subclass.nil? + @@resource2_api[credentials] ||= MU::Cloud::Google::Endpoint.new(api: "CloudresourcemanagerV2beta1::CloudResourceManagerService", scopes: ['https://www.googleapis.com/auth/cloud-platform'], credentials: credentials) + return @@resource2_api[credentials] + elsif subclass.is_a?(Symbol) + return Object.const_get("::Google").const_get("Apis").const_get("CloudresourcemanagerV2beta1").const_get(subclass) + end + end + # Google's Container API # @param subclass [<Google::Apis::ContainerV1>]: If specified, will return the class ::Google::Apis::ContainerV1::subclass instead of an API client instance def self.container(subclass = nil, credentials: nil) require 'google/apis/container_v1' @@ -907,9 +941,10 @@ @@storage_api = {} @@sql_api = {} @@iam_api = {} @@logging_api = {} @@resource_api = {} + @@resource2_api = {} @@service_api = {} @@admin_directory_api = {} end end end