v1 Set subscription policy (DEPRECATED)

(DEPRECATED) Set subscription policy

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Go API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "cloud.google.com/go/iam" 
  
 "cloud.google.com/go/pubsub" 
 ) 
 // addUsers adds all IAM users to a subscription. 
 func 
  
 addUsers 
 ( 
 projectID 
 , 
  
 subID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // subID := "my-sub" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 sub 
  
 := 
  
 client 
 . 
 Subscription 
 ( 
 subID 
 ) 
  
 policy 
 , 
  
 err 
  
 := 
  
 sub 
 . 
 IAM 
 (). 
 Policy 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "err getting IAM Policy: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 // Other valid prefixes are "serviceAccount:", "user:" 
  
 // See the documentation for more values. 
  
 policy 
 . 
  Add 
 
 ( 
 iam 
 . 
  AllUsers 
 
 , 
  
 iam 
 . 
  Viewer 
 
 ) 
  
 policy 
 . 
  Add 
 
 ( 
 "group:cloud-logs@google.com" 
 , 
  
 iam 
 . 
  Editor 
 
 ) 
  
 if 
  
 err 
  
 := 
  
 sub 
 . 
 IAM 
 (). 
 SetPolicy 
 ( 
 ctx 
 , 
  
 policy 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "SetPolicy: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 // NOTE: It may be necessary to retry this operation if IAM policies are 
  
 // being modified concurrently. SetPolicy will return an error if the policy 
  
 // was modified since it was retrieved. 
  
 return 
  
 nil 
 } 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Ruby API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # subscription_id       = "your-subscription-id" 
 # role                  = "roles/pubsub.publisher" 
 # service_account_email = "serviceAccount:account_name@project_name.iam.gserviceaccount.com" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Pubsub 
 
 . 
 new 
 subscription 
  
 = 
  
 pubsub 
 . 
 subscription 
  
 subscription_id 
 subscription 
 . 
 policy 
  
 do 
  
 | 
 policy 
 | 
  
 policy 
 . 
 add 
  
 role 
 , 
  
 service_account_email 
 end 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: