git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/client/container_get.go (about) 1 package client 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 8 v2container "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/container" 9 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" 10 rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc" 11 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" 12 v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session" 13 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature" 14 apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status" 15 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container" 16 cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" 17 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session" 18 ) 19 20 // PrmContainerGet groups parameters of ContainerGet operation. 21 type PrmContainerGet struct { 22 // FrostFS request X-Headers. 23 XHeaders []string 24 25 ContainerID *cid.ID 26 27 Session *session.Container 28 } 29 30 // SetContainer sets identifier of the container to be read. 31 // Required parameter. 32 // 33 // Deprecated: Use PrmContainerGet.ContainerID instead. 34 func (prm *PrmContainerGet) SetContainer(cid cid.ID) { 35 prm.ContainerID = &cid 36 } 37 38 func (prm *PrmContainerGet) buildRequest(c *Client) (*v2container.GetRequest, error) { 39 if prm.ContainerID == nil { 40 return nil, errorMissingContainer 41 } 42 43 if len(prm.XHeaders)%2 != 0 { 44 return nil, errorInvalidXHeaders 45 } 46 47 var cidV2 refs.ContainerID 48 prm.ContainerID.WriteToV2(&cidV2) 49 50 reqBody := new(v2container.GetRequestBody) 51 reqBody.SetContainerID(&cidV2) 52 53 var meta v2session.RequestMetaHeader 54 writeXHeadersToMeta(prm.XHeaders, &meta) 55 56 if prm.Session != nil { 57 var tokv2 v2session.Token 58 prm.Session.WriteToV2(&tokv2) 59 60 meta.SetSessionToken(&tokv2) 61 } 62 63 var req v2container.GetRequest 64 req.SetBody(reqBody) 65 c.prepareRequest(&req, &meta) 66 return &req, nil 67 } 68 69 // ResContainerGet groups resulting values of ContainerGet operation. 70 type ResContainerGet struct { 71 statusRes 72 73 cnr container.Container 74 } 75 76 // Container returns structured information about the requested container. 77 // 78 // Client doesn't retain value so modification is safe. 79 func (x ResContainerGet) Container() container.Container { 80 return x.cnr 81 } 82 83 // ContainerGet reads FrostFS container by ID. 84 // 85 // Exactly one return value is non-nil. By default, server status is returned in res structure. 86 // Any client's internal or transport errors are returned as `error`. 87 // If PrmInit.DisableFrostFSFailuresResolution has been called, unsuccessful 88 // FrostFS status codes are included in the returned result structure, 89 // otherwise, are also returned as `error`. 90 // 91 // Returns an error if parameters are set incorrectly (see PrmContainerGet docs). 92 // Context is required and must not be nil. It is used for network communication. 93 // 94 // Return statuses: 95 // - global (see Client docs); 96 // - *apistatus.ContainerNotFound. 97 func (c *Client) ContainerGet(ctx context.Context, prm PrmContainerGet) (*ResContainerGet, error) { 98 req, err := prm.buildRequest(c) 99 if err != nil { 100 return nil, err 101 } 102 103 if err := signature.SignServiceMessage(&c.prm.Key, req); err != nil { 104 return nil, fmt.Errorf("sign request: %w", err) 105 } 106 107 resp, err := rpcapi.GetContainer(&c.c, req, client.WithContext(ctx)) 108 if err != nil { 109 return nil, err 110 } 111 112 var res ResContainerGet 113 res.st, err = c.processResponse(resp) 114 if err != nil || !apistatus.IsSuccessful(res.st) { 115 return &res, err 116 } 117 118 cnrV2 := resp.GetBody().GetContainer() 119 if cnrV2 == nil { 120 return &res, errors.New("missing container in response") 121 } 122 if err := res.cnr.ReadFromV2(*cnrV2); err != nil { 123 return &res, fmt.Errorf("invalid container in response: %w", err) 124 } 125 return &res, nil 126 }