1 /*
2  * Copyright 2009-2017 Alibaba Cloud All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <alibabacloud/oss/model/SetBucketLifecycleRequest.h>
18 #include <alibabacloud/oss/Const.h>
19 #include <sstream>
20 #include <utils/Utils.h>
21 #include "ModelError.h"
22 
23 using namespace AlibabaCloud::OSS;
24 
SetBucketLifecycleRequest(const std::string & bucket)25 SetBucketLifecycleRequest::SetBucketLifecycleRequest(const std::string& bucket) :
26     OssBucketRequest(bucket)
27 {
28     setFlags(Flags() | REQUEST_FLAG_CONTENTMD5);
29 }
30 
payload() const31 std::string SetBucketLifecycleRequest::payload() const
32 {
33     std::stringstream ss;
34     ss << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
35     ss << "<LifecycleConfiguration>" << std::endl;
36     for (auto const &rule : lifecycleRules_)
37     {
38         ss << "  <Rule>" << std::endl;
39         ss << "    <ID>" << rule.ID() << "</ID>" << std::endl;
40         ss << "    <Prefix>" << rule.Prefix() << "</Prefix>" << std::endl;
41         for (const auto& tag : rule.Tags())
42         {
43             ss << "    <Tag><Key>" << tag.Key() << "</Key><Value>" << tag.Value() << "</Value></Tag>" << std::endl;
44         }
45         ss << "    <Status>" << ToRuleStatusName(rule.Status()) << "</Status>" << std::endl;
46         if (rule.hasExpiration())
47         {
48             ss << "    <Expiration>" << std::endl;
49             if (rule.Expiration().Days() > 0) {
50             ss << "      <Days>" << std::to_string(rule.Expiration().Days()) << "</Days>" << std::endl;
51             }
52             else if (!rule.Expiration().CreatedBeforeDate().empty()){
53             ss << "      <CreatedBeforeDate>" << rule.Expiration().CreatedBeforeDate() << "</CreatedBeforeDate>" << std::endl;
54             }
55             if (rule.ExpiredObjectDeleteMarker()) {
56             ss << "      <ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker>" << std::endl;
57             }
58             ss << "    </Expiration>" << std::endl;
59         }
60         for (auto const & transition: rule.TransitionList())
61         {
62             ss << "    <Transition>" << std::endl;
63             if (transition.Expiration().Days() > 0) {
64             ss << "      <Days>" << std::to_string(transition.Expiration().Days()) << "</Days>" << std::endl;
65             }
66             else {
67             ss << "      <CreatedBeforeDate>" << transition.Expiration().CreatedBeforeDate() << "</CreatedBeforeDate>" << std::endl;
68             }
69             ss << "      <StorageClass>" << ToStorageClassName(transition.StorageClass()) << "</StorageClass>" << std::endl;
70             ss << "    </Transition>" << std::endl;
71         }
72         if (rule.hasAbortMultipartUpload())
73         {
74             ss << "    <AbortMultipartUpload>" << std::endl;
75             if (rule.AbortMultipartUpload().Days() > 0) {
76             ss << "      <Days>" << std::to_string(rule.AbortMultipartUpload().Days()) << "</Days>" << std::endl;
77             }
78             else {
79             ss << "      <CreatedBeforeDate>" << rule.AbortMultipartUpload().CreatedBeforeDate() << "</CreatedBeforeDate>" << std::endl;
80             }
81             ss << "    </AbortMultipartUpload>" << std::endl;
82         }
83         if (rule.hasNoncurrentVersionExpiration())
84         {
85             ss << "    <NoncurrentVersionExpiration>" << std::endl;
86             ss << "      <NoncurrentDays>" << std::to_string(rule.NoncurrentVersionExpiration().Days()) << "</NoncurrentDays>" << std::endl;
87             ss << "    </NoncurrentVersionExpiration>" << std::endl;
88         }
89         for (auto const & transition : rule.NoncurrentVersionTransitionList())
90         {
91             ss << "    <NoncurrentVersionTransition>" << std::endl;
92             if (transition.Expiration().Days() > 0) {
93             ss << "      <NoncurrentDays>" << std::to_string(transition.Expiration().Days()) << "</NoncurrentDays>" << std::endl;
94             }
95             ss << "      <StorageClass>" << ToStorageClassName(transition.StorageClass()) << "</StorageClass>" << std::endl;
96             ss << "    </NoncurrentVersionTransition>" << std::endl;
97         }
98         ss << "  </Rule>" << std::endl;
99     }
100     ss << "</LifecycleConfiguration>" << std::endl;
101     return ss.str();
102 }
103 
specialParameters() const104 ParameterCollection SetBucketLifecycleRequest::specialParameters() const
105 {
106     ParameterCollection parameters;
107     parameters["lifecycle"] = "";
108     return parameters;
109 }
110 
validate() const111 int SetBucketLifecycleRequest::validate() const
112 {
113     int ret;
114     if ((ret = OssBucketRequest::validate()) != 0) {
115         return ret;
116     }
117 
118     if (lifecycleRules_.size() > LifecycleRuleLimit) {
119         return ARG_ERROR_LIFECYCLE_RULE_LIMIT;
120     }
121 
122     if (lifecycleRules_.empty()) {
123         return ARG_ERROR_LIFECYCLE_RULE_EMPTY;
124     }
125 
126     for (auto const &rule : lifecycleRules_) {
127         //no config rule
128         if (!rule.hasAbortMultipartUpload() &&
129             !rule.hasExpiration() &&
130             !rule.hasTransitionList() &&
131             !rule.hasNoncurrentVersionExpiration() &&
132             !rule.hasNoncurrentVersionTransitionList()) {
133             return ARG_ERROR_LIFECYCLE_RULE_CONFIG_EMPTY;
134         }
135 
136         if (rule.Prefix().empty() && lifecycleRules_.size() > 1) {
137             return ARG_ERROR_LIFECYCLE_RULE_ONLY_ONE_FOR_BUCKET;
138         }
139     }
140 
141     return 0;
142 }
143