Copyright 2017 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package storage

import (
	

	
	
	raw 
	iampb 
	
)
IAM provides access to IAM access control for the bucket.
iamClient implements the iam.client interface.
type iamClient struct {
	raw         *raw.Service
	userProject string
}

func ( *iamClient) ( context.Context,  string) ( *iampb.Policy,  error) {
	return .GetWithVersion(, , 1)
}

func ( *iamClient) ( context.Context,  string,  int32) ( *iampb.Policy,  error) {
	 = trace.StartSpan(, "cloud.google.com/go/storage.IAM.Get")
	defer func() { trace.EndSpan(, ) }()

	 := .raw.Buckets.GetIamPolicy().OptionsRequestedPolicyVersion(int64())
	setClientHeader(.Header())
	if .userProject != "" {
		.UserProject(.userProject)
	}
	var  *raw.Policy
	 = runWithRetry(, func() error {
		,  = .Context().Do()
		return 
	})
	if  != nil {
		return nil, 
	}
	return iamFromStoragePolicy(), nil
}

func ( *iamClient) ( context.Context,  string,  *iampb.Policy) ( error) {
	 = trace.StartSpan(, "cloud.google.com/go/storage.IAM.Set")
	defer func() { trace.EndSpan(, ) }()

	 := iamToStoragePolicy()
	 := .raw.Buckets.SetIamPolicy(, )
	setClientHeader(.Header())
	if .userProject != "" {
		.UserProject(.userProject)
	}
	return runWithRetry(, func() error {
		,  := .Context().Do()
		return 
	})
}

func ( *iamClient) ( context.Context,  string,  []string) ( []string,  error) {
	 = trace.StartSpan(, "cloud.google.com/go/storage.IAM.Test")
	defer func() { trace.EndSpan(, ) }()

	 := .raw.Buckets.TestIamPermissions(, )
	setClientHeader(.Header())
	if .userProject != "" {
		.UserProject(.userProject)
	}
	var  *raw.TestIamPermissionsResponse
	 = runWithRetry(, func() error {
		,  = .Context().Do()
		return 
	})
	if  != nil {
		return nil, 
	}
	return .Permissions, nil
}

func ( *iampb.Policy) *raw.Policy {
	return &raw.Policy{
		Bindings: iamToStorageBindings(.Bindings),
		Etag:     string(.Etag),
		Version:  int64(.Version),
	}
}

func ( []*iampb.Binding) []*raw.PolicyBindings {
	var  []*raw.PolicyBindings
	for ,  := range  {
		 = append(, &raw.PolicyBindings{
			Role:      .Role,
			Members:   .Members,
			Condition: iamToStorageCondition(.Condition),
		})
	}
	return 
}

func ( *expr.Expr) *raw.Expr {
	if  == nil {
		return nil
	}
	return &raw.Expr{
		Expression:  .Expression,
		Description: .Description,
		Location:    .Location,
		Title:       .Title,
	}
}

func ( *raw.Policy) *iampb.Policy {
	return &iampb.Policy{
		Bindings: iamFromStorageBindings(.Bindings),
		Etag:     []byte(.Etag),
	}
}

func ( []*raw.PolicyBindings) []*iampb.Binding {
	var  []*iampb.Binding
	for ,  := range  {
		 = append(, &iampb.Binding{
			Role:      .Role,
			Members:   .Members,
			Condition: iamFromStorageCondition(.Condition),
		})
	}
	return 
}

func ( *raw.Expr) *expr.Expr {
	if  == nil {
		return nil
	}
	return &expr.Expr{
		Expression:  .Expression,
		Description: .Description,
		Location:    .Location,
		Title:       .Title,
	}