@@ -134,6 +134,10 @@ func logicalViewPath(project, instance, logicalView string) string {
134134 return fmt .Sprintf ("%s/logicalViews/%s" , instancePrefix (project , instance ), logicalView )
135135}
136136
137+ func materializedlViewPath (project , instance , materializedView string ) string {
138+ return fmt .Sprintf ("%s/materializedViews/%s" , instancePrefix (project , instance ), materializedView )
139+ }
140+
137141// EncryptionInfo represents the encryption info of a table.
138142type EncryptionInfo struct {
139143 Status * Status
@@ -2788,8 +2792,13 @@ func (iac *InstanceAdminClient) CreateLogicalView(ctx context.Context, instanceI
27882792 Query : conf .Query ,
27892793 },
27902794 }
2791- _ , err := iac .iClient .CreateLogicalView (ctx , req )
2792- return err
2795+
2796+ op , err := iac .iClient .CreateLogicalView (ctx , req )
2797+ if err != nil {
2798+ return err
2799+ }
2800+ resp := btapb.LogicalView {}
2801+ return longrunning .InternalNewOperation (iac .lroClient , op ).Wait (ctx , & resp )
27932802}
27942803
27952804// LogicalViewInfo contains logical view metadata. This struct is read-only.
@@ -2883,3 +2892,155 @@ func (iac *InstanceAdminClient) DeleteLogicalView(ctx context.Context, instanceI
28832892 _ , err := iac .iClient .DeleteLogicalView (ctx , req )
28842893 return err
28852894}
2895+
2896+ // Materialized Views
2897+
2898+ // CreateMaterializedView creates a new materialized view in an instance.
2899+ func (iac * InstanceAdminClient ) CreateMaterializedView (ctx context.Context , instanceID string , conf * MaterializedViewInfo ) error {
2900+ if conf .MaterializedViewID == "" {
2901+ return errors .New ("MaterializedViewID is required" )
2902+ }
2903+
2904+ ctx = mergeOutgoingMetadata (ctx , iac .md )
2905+ mv := & btapb.MaterializedView {
2906+ Query : conf .Query ,
2907+ }
2908+ if conf .DeletionProtection != None {
2909+ switch dp := conf .DeletionProtection ; dp {
2910+ case Protected :
2911+ mv .DeletionProtection = true
2912+ case Unprotected :
2913+ mv .DeletionProtection = false
2914+ default :
2915+ break
2916+ }
2917+ }
2918+ req := & btapb.CreateMaterializedViewRequest {
2919+ Parent : instancePrefix (iac .project , instanceID ),
2920+ MaterializedViewId : conf .MaterializedViewID ,
2921+ MaterializedView : mv ,
2922+ }
2923+ op , err := iac .iClient .CreateMaterializedView (ctx , req )
2924+ if err != nil {
2925+ return err
2926+ }
2927+ resp := btapb.MaterializedView {}
2928+ return longrunning .InternalNewOperation (iac .lroClient , op ).Wait (ctx , & resp )
2929+ }
2930+
2931+ // MaterializedViewInfo contains materialized view metadata. This struct is read-only.
2932+ type MaterializedViewInfo struct {
2933+ MaterializedViewID string
2934+
2935+ Query string
2936+ DeletionProtection DeletionProtection
2937+ }
2938+
2939+ // MaterializedViewInfo retrieves information about a materialized view.
2940+ func (iac * InstanceAdminClient ) MaterializedViewInfo (ctx context.Context , instanceID , materializedViewID string ) (* MaterializedViewInfo , error ) {
2941+ ctx = mergeOutgoingMetadata (ctx , iac .md )
2942+ prefix := instancePrefix (iac .project , instanceID )
2943+ req := & btapb.GetMaterializedViewRequest {
2944+ Name : materializedlViewPath (iac .project , instanceID , materializedViewID ),
2945+ }
2946+ var res * btapb.MaterializedView
2947+
2948+ err := gax .Invoke (ctx , func (ctx context.Context , _ gax.CallSettings ) error {
2949+ var err error
2950+ res , err = iac .iClient .GetMaterializedView (ctx , req )
2951+ return err
2952+ }, retryOptions ... )
2953+
2954+ if err != nil {
2955+ return nil , err
2956+ }
2957+ mv := & MaterializedViewInfo {MaterializedViewID : strings .TrimPrefix (res .Name , prefix + "/materializedViews/" ), Query : res .Query }
2958+ if res .DeletionProtection {
2959+ mv .DeletionProtection = Protected
2960+ } else {
2961+ mv .DeletionProtection = Unprotected
2962+ }
2963+ return mv , nil
2964+ }
2965+
2966+ // MaterializedViews returns a list of the materialized views in the instance.
2967+ func (iac * InstanceAdminClient ) MaterializedViews (ctx context.Context , instanceID string ) ([]MaterializedViewInfo , error ) {
2968+ views := []MaterializedViewInfo {}
2969+ prefix := instancePrefix (iac .project , instanceID )
2970+ req := & btapb.ListMaterializedViewsRequest {
2971+ Parent : prefix ,
2972+ }
2973+ var res * btapb.ListMaterializedViewsResponse
2974+ err := gax .Invoke (ctx , func (ctx context.Context , _ gax.CallSettings ) error {
2975+ var err error
2976+ res , err = iac .iClient .ListMaterializedViews (ctx , req )
2977+ return err
2978+ }, retryOptions ... )
2979+ if err != nil {
2980+ return nil , err
2981+ }
2982+
2983+ for _ , mView := range res .MaterializedViews {
2984+ mv := MaterializedViewInfo {MaterializedViewID : strings .TrimPrefix (mView .Name , prefix + "/materializedViews/" ), Query : mView .Query }
2985+ if mView .DeletionProtection {
2986+ mv .DeletionProtection = Protected
2987+ } else {
2988+ mv .DeletionProtection = Unprotected
2989+ }
2990+ views = append (views , mv )
2991+ }
2992+ return views , nil
2993+ }
2994+
2995+ // UpdateMaterializedView updates a materialized view in an instance according to the given configuration.
2996+ func (iac * InstanceAdminClient ) UpdateMaterializedView (ctx context.Context , instanceID string , conf MaterializedViewInfo ) error {
2997+ ctx = mergeOutgoingMetadata (ctx , iac .md )
2998+ if conf .MaterializedViewID == "" {
2999+ return errors .New ("MaterializedViewID is required" )
3000+ }
3001+ mv := & btapb.MaterializedView {}
3002+ mv .Name = materializedlViewPath (iac .project , instanceID , conf .MaterializedViewID )
3003+
3004+ updateMask := & field_mask.FieldMask {
3005+ Paths : []string {},
3006+ }
3007+ if conf .Query != "" {
3008+ updateMask .Paths = append (updateMask .Paths , "query" )
3009+ mv .Query = conf .Query
3010+ }
3011+ if conf .DeletionProtection != None {
3012+ updateMask .Paths = append (updateMask .Paths , "deletion_protection" )
3013+ switch dp := conf .DeletionProtection ; dp {
3014+ case Protected :
3015+ mv .DeletionProtection = true
3016+ case Unprotected :
3017+ mv .DeletionProtection = false
3018+ default :
3019+ break
3020+ }
3021+ }
3022+ req := & btapb.UpdateMaterializedViewRequest {
3023+ MaterializedView : mv ,
3024+ UpdateMask : updateMask ,
3025+ }
3026+ lro , err := iac .iClient .UpdateMaterializedView (ctx , req )
3027+ if err != nil {
3028+ return fmt .Errorf ("error from update materialized view: %w" , err )
3029+ }
3030+ var res btapb.MaterializedView
3031+ op := longrunning .InternalNewOperation (iac .lroClient , lro )
3032+ if err = op .Wait (ctx , & res ); err != nil {
3033+ return fmt .Errorf ("error from operation: %v" , err )
3034+ }
3035+ return nil
3036+ }
3037+
3038+ // DeleteMaterializedView deletes a materialized view in an instance.
3039+ func (iac * InstanceAdminClient ) DeleteMaterializedView (ctx context.Context , instanceID , materializedViewID string ) error {
3040+ ctx = mergeOutgoingMetadata (ctx , iac .md )
3041+ req := & btapb.DeleteMaterializedViewRequest {
3042+ Name : materializedlViewPath (iac .project , instanceID , materializedViewID ),
3043+ }
3044+ _ , err := iac .iClient .DeleteMaterializedView (ctx , req )
3045+ return err
3046+ }
0 commit comments