def KubernetesConfig(self): """Create a kubernetes config file. Returns: Text of a kubernetes config file. """ deployment = local.CreateDeployment(self._settings.service_name, self._settings.image_name) if self._settings.env_vars: local.AddEnvironmentVariables( deployment, self._settings.service_name + '-container', self._settings.env_vars) kubernetes_configs = [ deployment, local.CreateService(self._settings.service_name) ] if self._settings.service_account: service_account = local.CreateDevelopmentServiceAccount( self._settings.service_account) private_key_json = local.CreateServiceAccountKey(service_account) secret_yaml = local.LocalDevelopmentSecretSpec(private_key_json) kubernetes_configs.append(secret_yaml) local.AddServiceAccountSecret(kubernetes_configs) return yaml.dump_all(kubernetes_configs)
def testComputeServiceAccountMissing(self): self.mock_iam_client.projects_serviceAccounts.Get.Expect( mock.ANY, exception=apitools_exceptions.HttpNotFoundError('', '', ''), enable_type_checking=False) with self.assertRaises(ValueError): local.CreateDevelopmentServiceAccount( '*****@*****.**')
def testAppengineServiceAccountExists(self): self.mock_iam_client.projects_serviceAccounts.Get.Expect( mock.ANY, response=IAM_MESSAGE_MODULE.ServiceAccount(), enable_type_checking=False) self.assertEqual( local.CreateDevelopmentServiceAccount( '*****@*****.**'), 'projects/my-project/serviceAccounts/' '*****@*****.**')
def testServiceAccountAlreadyExists(self): self.mock_iam_client.projects_serviceAccounts.Get.Expect( mock.ANY, response=IAM_MESSAGE_MODULE.ServiceAccount(), enable_type_checking=False) self.assertEqual( local.CreateDevelopmentServiceAccount( '*****@*****.**'), 'projects/project-id/serviceAccounts/id-exists@' 'project-id.iam.gserviceaccount.com')
def testCreateDevelopmentServiceAccount(self): self.mock_iam_client.projects_serviceAccounts.Get.Expect( mock.ANY, exception=apitools_exceptions.HttpNotFoundError('', '', ''), enable_type_checking=False) service_account_msg = IAM_MESSAGE_MODULE.ServiceAccount( displayName='Serverless Local Development Service Account') create_account_request = IAM_MESSAGE_MODULE.CreateServiceAccountRequest( accountId='my-id', serviceAccount=service_account_msg) request = IAM_MESSAGE_MODULE.IamProjectsServiceAccountsCreateRequest( name='projects/project-id', createServiceAccountRequest=create_account_request) self.mock_iam_client.projects_serviceAccounts.Create.Expect( request, response=IAM_MESSAGE_MODULE.ServiceAccount()) self.mock_crm_client.projects.GetIamPolicy.Expect( CRM_MESSAGE_MODULE.CloudresourcemanagerProjectsGetIamPolicyRequest( resource='project-id'), response=CRM_MESSAGE_MODULE.Policy()) self.mock_crm_client.projects.SetIamPolicy.Expect( CRM_MESSAGE_MODULE.CloudresourcemanagerProjectsSetIamPolicyRequest( resource='project-id', setIamPolicyRequest=CRM_MESSAGE_MODULE.SetIamPolicyRequest( policy=CRM_MESSAGE_MODULE.Policy(bindings=[ CRM_MESSAGE_MODULE.Binding( members=[ 'serviceAccount:my-id@' 'project-id.iam.gserviceaccount.com', ], role='roles/editor', ), ]), )), response=CRM_MESSAGE_MODULE.Policy(bindings=[ CRM_MESSAGE_MODULE.Binding( members=[ 'serviceAccount:my-id@' 'project-id.iam.gserviceaccount.com', ], role='roles/editor', ), ])) self.assertEqual( local.CreateDevelopmentServiceAccount( '*****@*****.**'), 'projects/project-id/serviceAccounts/my-id@' 'project-id.iam.gserviceaccount.com')
def KubernetesConfig(self): """Create a kubernetes config file. Returns: Text of a kubernetes config file. """ kubernetes_configs = local.CreatePodAndService(self._settings.service_name, self._settings.image_name) if self._settings.service_account: service_account = local.CreateDevelopmentServiceAccount( self._settings.service_account) private_key_json = local.CreateServiceAccountKey(service_account) secret_yaml = local.LocalDevelopmentSecretSpec(private_key_json) kubernetes_configs.append(secret_yaml) local.AddServiceAccountSecret(kubernetes_configs) return yaml.dump_all(kubernetes_configs)
def testComputeServiceAccountExists(self): self.mock_iam_client.projects_serviceAccounts.Get.Expect( mock.ANY, response=IAM_MESSAGE_MODULE.ServiceAccount(), enable_type_checking=False) self.mock_crm_client.projects.Get.Expect( mock.ANY, response=CRM_MESSAGE_MODULE.Project( projectId=six.ensure_binary('my-project')), enable_type_checking=False) self.assertEqual( local.CreateDevelopmentServiceAccount( '*****@*****.**'), 'projects/my-project/serviceAccounts/' '*****@*****.**')
def testCreateDevelopmentServiceAccountNoEditorRole(self): self.mock_iam_client.projects_serviceAccounts.Get.Expect( mock.ANY, exception=apitools_exceptions.HttpNotFoundError('', '', ''), enable_type_checking=False) service_account_msg = IAM_MESSAGE_MODULE.ServiceAccount( displayName='Serverless Local Development Service Account') create_account_request = IAM_MESSAGE_MODULE.CreateServiceAccountRequest( accountId='my-id', serviceAccount=service_account_msg) request = IAM_MESSAGE_MODULE.IamProjectsServiceAccountsCreateRequest( name='projects/project-id', createServiceAccountRequest=create_account_request) self.mock_iam_client.projects_serviceAccounts.Create.Expect( request, response=IAM_MESSAGE_MODULE.ServiceAccount()) self.WriteInput('n') self.assertEqual( local.CreateDevelopmentServiceAccount( '*****@*****.**'), 'projects/project-id/serviceAccounts/my-id@' 'project-id.iam.gserviceaccount.com')