public async Task GetProjectsListAsyncTestMultiPage() { var responses = new[] { new ListProjectsResponse { Projects = new List <Project> { s_someProject, s_disabledProject }, NextPageToken = "2" }, new ListProjectsResponse { Projects = new List <Project> { s_aProject }, NextPageToken = null } }; CloudResourceManagerService service = GetMockedService( (CloudResourceManagerService s) => s.Projects, p => p.List(), responses); var dataSource = new ResourceManagerDataSource(null, init => service, null); IList <Project> projects = await dataSource.GetProjectsListAsync(); Assert.AreEqual(3, projects.Count); Assert.AreEqual(s_someProject, projects[0]); Assert.AreEqual(s_disabledProject, projects[1]); Assert.AreEqual(s_aProject, projects[2]); }
public async Task RefreshProjects_RestartsProjectsListTask() { var responses = new[] { new ListProjectsResponse { Projects = new List <Project> { s_someProject, s_disabledProject }, NextPageToken = null }, new ListProjectsResponse { Projects = new List <Project> { s_aProject }, NextPageToken = null } }; CloudResourceManagerService service = GetMockedService( (CloudResourceManagerService s) => s.Projects, p => p.List(), responses); var dataSource = new ResourceManagerDataSource(null, init => service, null); dataSource.RefreshProjects(); IList <Project> projects = await dataSource.ProjectsListTask; Assert.AreEqual(1, projects.Count); Assert.AreEqual(s_aProject, projects[0]); }
public static void Main(string[] args) { // TODO: Replace with your project ID var projectId = "your-project"; // TODO: Replace with the ID of your member in the form "user:[email protected]" var member = "your-member"; // Role to be granted var role = "roles/logging.logWriter"; // Initialize service CloudResourceManagerService crmService = InitializeService(); // Grant your member the "Log Writer" role for your project AddBinding(crmService, projectId, member, role); // Get the project's policy and print all members with the the "Log Writer" role var policy = GetPolicy(crmService, projectId); var binding = policy.Bindings.FirstOrDefault(x => x.Role == role); Console.WriteLine("Role: " + binding.Role); Console.Write("Members: "); foreach (var m in binding.Members) { Console.Write("[" + m + "] "); } Console.WriteLine(); // Remove member from the "Log Writer" role RemoveMember(crmService, projectId, member, role); }
public static void AddBinding( CloudResourceManagerService crmService, string projectId, string member, string role) { // Get the project's policy var policy = GetPolicy(crmService, projectId); // Find binding in policy var binding = policy.Bindings.FirstOrDefault(x => x.Role == role); // If binding already exists, add member to binding if (binding != null) { binding.Members.Add(member); } // If binding does not exist, add binding to policy else { binding = new Binding { Role = role, Members = new List <string> { member } }; policy.Bindings.Add(binding); } // Set the updated policy SetPolicy(crmService, projectId, policy); }
static void Main(string[] args) { GoogleCredential credential = GoogleCredential.GetApplicationDefault(); if (credential.IsCreateScopedRequired) { credential = credential.CreateScoped(new[] { CloudResourceManagerService.Scope.CloudPlatformReadOnly }); } var crmService = new CloudResourceManagerService(new BaseClientService.Initializer() { HttpClientInitializer = credential, }); var request = new ProjectsResource.ListRequest(crmService); while (true) { var result = request.Execute(); foreach (var project in result.Projects) { Console.WriteLine(project.ProjectId); } if (string.IsNullOrEmpty(result.NextPageToken)) { break; } request.PageToken = result.NextPageToken; } }
public ResourceManagerAdapter(IAuthorizationAdapter authService) { this.service = new CloudResourceManagerService( new BaseClientService.Initializer { HttpClientInitializer = authService.Authorization.Credential, ApplicationName = Globals.UserAgent.ToApplicationName() }); }
public ResourceManagerAdapter(ICredential credential) { this.service = new CloudResourceManagerService( new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = Globals.UserAgent.ToApplicationName() }); }
public static void SetPolicy(CloudResourceManagerService crmService, String projectId, Policy policy) { // Set the project's policy by calling the // Cloud Resource Manager Projects API crmService.Projects.SetIamPolicy( new SetIamPolicyRequest { Policy = policy }, projectId).Execute(); }
public static Policy GetPolicy(CloudResourceManagerService crmService, String projectId) { // Get the project's policy by calling the // Cloud Resource Manager Projects API var policy = crmService.Projects.GetIamPolicy( new GetIamPolicyRequest(), projectId).Execute(); return(policy); }
public async Task GetProjectTestException() { // Empty response list triggers GoogleApiException. var responses = new Project[0]; CloudResourceManagerService service = GetMockedService( (CloudResourceManagerService s) => s.Projects, p => p.Get(It.IsAny <string>()), responses); var dataSource = new ResourceManagerDataSource(null, init => service, null); await dataSource.GetProjectAsync(SomeProjectId); }
public async Task GetProjectsListAsyncTestException() { // Empty response list triggers GoogleApiException. var responses = new ListProjectsResponse[0]; CloudResourceManagerService service = GetMockedService( (CloudResourceManagerService s) => s.Projects, p => p.List(), responses); var dataSource = new ResourceManagerDataSource(null, init => service, null); await dataSource.GetProjectsListAsync(); }
public ResourceManagerAdapter(IAuthorizationAdapter authService) { var assemblyName = typeof(IResourceManagerAdapter).Assembly.GetName(); this.service = new CloudResourceManagerService( new BaseClientService.Initializer { HttpClientInitializer = authService.Authorization.Credential, ApplicationName = $"{assemblyName.Name}/{assemblyName.Version}" }); }
public AccessManager() { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(CloudResourceManagerService.Scope.CloudPlatform); _service = new CloudResourceManagerService( new CloudResourceManagerService.Initializer { HttpClientInitializer = credential }); }
public CloudResourceManagerService GetCloudResourceManagerService() { CloudResourceManagerService service = new CloudResourceManagerService( new BaseClientService.Initializer() { HttpClientInitializer = _authorizationCodeApp.Credential, ApplicationName = _applicationName } ); return(service); }
private async Task <(IList <ProjectNetworkModel>, IList <string[]>)> RetrieveProjectNetworkTopologies( string accessToken = null, string projectId = null, string outputFile = "network_topology.json", bool skipDefault = false) { var result = new List <ProjectNetworkModel>(); var errors = new List <string[]>(); var service = new CloudResourceManagerService(createInitializer(accessToken)); IList <Google.Apis.CloudResourceManager.v1.Data.Project> projects = new List <Google.Apis.CloudResourceManager.v1.Data.Project>(); if (projectId != null) { Console.WriteLine("Generating network topology for Project: {0}", projectId); var retrievedProject = await service.Projects.Get(projectId).ExecuteAsync(); if (retrievedProject == null) { Console.WriteLine("Could not retrieve Project: {0}", projectId); return(result, errors); } projects.Add(retrievedProject); } else { Console.WriteLine("Generating network topology for all Projects"); var retrievedProjects = await service.Projects.List().ExecuteAsync(); projects = retrievedProjects.Projects; } foreach (var project in projects) { try { Console.WriteLine("Retrieving network topology for Project: {0}", project.ProjectId); var networksForProject = await RetrieveNetworkTopology(accessToken, project.ProjectId, skipDefault); var model = asModel(project, networksForProject); result.Add(model); } catch (Exception cause) { errors.Add(new string[] { project.ProjectId, cause.Message }); } } return(result, errors); }
public static CloudResourceManagerService InitializeService() { // Get credentials var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(IamService.Scope.CloudPlatform); // Create the Cloud Resource Manager service object CloudResourceManagerService crmService = new CloudResourceManagerService( new CloudResourceManagerService.Initializer { HttpClientInitializer = credential }); return(crmService); }
public static Policy SetPolicy(string projectId, Policy policy) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(CloudResourceManagerService.Scope.CloudPlatform); var service = new CloudResourceManagerService( new CloudResourceManagerService.Initializer { HttpClientInitializer = credential }); return(service.Projects.SetIamPolicy(new SetIamPolicyRequest { Policy = policy }, projectId).Execute()); }
public ResourceManagerAdapter( ICredential credential, IDeviceEnrollment deviceEnrollment) { this.service = new CloudResourceManagerService( ClientServiceFactory.ForMtlsEndpoint( credential, deviceEnrollment, MtlsBaseUri)); Debug.Assert( (deviceEnrollment?.Certificate != null && HttpClientHandlerExtensions.IsClientCertificateSupported) == IsDeviceCertiticateAuthenticationEnabled); }
public PubsubFixture() { GrpcInfo.EnableSubchannelCounting(); _projectNumber = new Lazy <Task <string> >(async() => { var cred = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false); cred = cred.CreateScoped(CloudResourceManagerService.Scope.CloudPlatformReadOnly); var crm = new CloudResourceManagerService(new BaseClientService.Initializer { HttpClientInitializer = cred, ApplicationName = "pubsub integration test", }); var project = await crm.Projects.Get(ProjectId).ExecuteAsync().ConfigureAwait(false); return(project.ProjectNumber.ToString()); }); }
public IHttpActionResult CreateProject(string projectID, string projectName) { var service = new CloudResourceManagerService(new BaseClientService.Initializer { HttpClientInitializer = AppFlowMetadata.UserCredential, ApplicationName = AppFlowMetadata.AppName, }); Data.Project requestBody = new Data.Project() { ProjectId = projectID, Name = projectName }; ProjectsResource.CreateRequest request = service.Projects.Create(requestBody); var response = request.Execute(); return(Ok()); }
/// <summary> /// Fetches an Organization resource identified by the specified resource name. /// Documentation https://developers.google.com/cloudresourcemanager/v1/reference/organizations/get /// Generation Note: This does not always build corectly. Google needs to standardise things I need to figuer out which ones are wrong. /// </summary> /// <param name="service">Authenticated CloudResourceManager service.</param> /// <param name="name">The resource name of the Organization to fetch, e.g. "organizations/1234".</param> /// <returns>OrganizationResponse</returns> public static Organization Get(CloudResourceManagerService service, string name) { try { // Initial validation. if (service == null) throw new ArgumentNullException("service"); if (name == null) throw new ArgumentNullException(name); // Make the request. return service.Organizations.Get(name).Execute(); } catch (Exception ex) { throw new Exception("Request Organizations.Get failed.", ex); } }
public async Task GetProjectTestSuccess() { var responses = new[] { s_someProject }; CloudResourceManagerService service = GetMockedService( (CloudResourceManagerService s) => s.Projects, p => p.Get(It.IsAny <string>()), responses); var dataSource = new ResourceManagerDataSource(null, init => service, null); Project project = await dataSource.GetProjectAsync(SomeProjectId); Assert.AreEqual(s_someProject, project); Mock <ProjectsResource> projectsResource = Mock.Get(service.Projects); projectsResource.Verify(r => r.Get(SomeProjectId), Times.Once); projectsResource.Verify(r => r.Get(It.IsNotIn(SomeProjectId)), Times.Never); }
/// <param name="body">A valid CloudResourceManager v1beta1 body.</param> /// <returns>GetAncestryResponseResponse</returns> public static GetAncestryResponse GetAncestry(CloudResourceManagerService service, string projectId, GetAncestryRequest body) { try { // Initial validation. if (service == null) throw new ArgumentNullException("service"); if (body == null) throw new ArgumentNullException("body"); if (projectId == null) throw new ArgumentNullException(projectId); // Make the request. return service.Projects.GetAncestry(body, projectId).Execute(); } catch (Exception ex) { throw new Exception("Request Projects.GetAncestry failed.", ex); } }
public static IList <String> TestIamPermissions(string projectId) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(CloudResourceManagerService.Scope.CloudPlatform); var service = new CloudResourceManagerService( new CloudResourceManagerService.Initializer { HttpClientInitializer = credential }); TestIamPermissionsRequest requestBody = new TestIamPermissionsRequest(); var permissions = new List <string>() { "resourcemanager.projects.get", "resourcemanager.projects.delete" }; requestBody.Permissions = new List <string>(permissions); var returnedPermissions = service.Projects.TestIamPermissions(requestBody, projectId).Execute().Permissions; return(returnedPermissions); }
private bool CreateProject(UserCredential userCredential, string projectID, string projectName) { try { var service = new CloudResourceManagerService(new BaseClientService.Initializer { HttpClientInitializer = userCredential, ApplicationName = AppName, }); Data.Project requestBody = new Data.Project() { ProjectId = projectID, Name = projectName }; Google.Apis.CloudResourceManager.v1.ProjectsResource.CreateRequest request = service.Projects.Create(requestBody); var response = request.Execute(); } catch (Exception) { } return(true); }
public static void RemoveMember( CloudResourceManagerService crmService, string projectId, string member, string role) { // Get the project's policy var policy = GetPolicy(crmService, projectId); // Remove the member from the role var binding = policy.Bindings.FirstOrDefault(x => x.Role == role); if (binding == null) { Console.WriteLine("Role does not exist in policy."); } else { if (binding.Members.Contains(member)) { binding.Members.Remove(member); } else { Console.WriteLine("The member has not been granted this role."); } if (binding.Members.Count == 0) { policy.Bindings.Remove(binding); } } // Set the updated policy SetPolicy(crmService, projectId, policy); }
public CloudResourceManagerCmdlet() { Service = new CloudResourceManagerService(GetBaseClientServiceInitializer()); }