ucloud/terraform-provider-ucloud

TestAccUCloudCubePod_basic failed

lonegunmanb opened this issue · 4 comments

git version: 256bac0
Run test TestAccUCloudCubePod_basic after environment variables properly set, the test failed with following output:

time="2021-05-26T10:31:44+08:00" level=debug msg="[2a39391a-d9c2-4d7a-ac78-c38f4a87f17e] https://api.ucloud.cn?Action=DescribeVPC Action=DescribeVPC&Limit=100&Offset=0&ProjectId=org-xehypo&PublicKey=JInqRnkSY8eAmxKFRxW9kVANYThbzxJKNMEWhQ54N&Region=hk&Signature=31f01e334210679ae8e4923a2836b310c4b310d7"
time="2021-05-26T10:31:44+08:00" level=debug msg="[2a39391a-d9c2-4d7a-ac78-c38f4a87f17e] {"Action":"DescribeVPCResponse","DataSet":[{"CreateTime":1562309255,"IPv6Network":"","Name":"DefaultVPC","Network":["10.8.0.0/16","10.7.0.0/16"],"NetworkInfo":[{"Network":"10.8.0.0/16","SubnetCount":1},{"Network":"10.7.0.0/16","SubnetCount":1}],"OperatorName":"","Remark":"","SubnetCount":2,"Tag":"Default","TunnelId":68123391,"UpdateTime":1562309256,"VPCId":"uvnet-ecrfor2j"},{"CreateTime":1562046410,"IPv6Network":"","Name":"DefaultVPC","Network":["10.8.0.0/16","10.7.0.0/16"],"NetworkInfo":[{"Network":"10.8.0.0/16","SubnetCount":1},{"Network":"10.7.0.0/16","SubnetCount":1}],"OperatorName":"","Remark":"","SubnetCount":2,"Tag":"Default","TunnelId":68118106,"UpdateTime":1562046411,"VPCId":"uvnet-vjyd5t1n"}],"RetCode":0,"TotalCount":2} - 200"
time="2021-05-26T10:31:44+08:00" level=info msg="[2a39391a-d9c2-4d7a-ac78-c38f4a87f17e] do DescribeVPC successful!"
time="2021-05-26T10:31:44+08:00" level=debug msg="[78178e47-fa21-488d-a6c3-0af5cfdb3303] https://api.ucloud.cn?Action=DescribeSubnet Action=DescribeSubnet&Limit=100&Offset=0&ProjectId=org-xehypo&PublicKey=JInqRnkSY8eAmxKFRxW9kVANYThbzxJKNMEWhQ54N&Region=hk&Signature=4c6c89a500e87918518068c31ffc95c0272c50e4&VPCId=uvnet-ecrfor2j"
time="2021-05-26T10:31:44+08:00" level=debug msg="[78178e47-fa21-488d-a6c3-0af5cfdb3303] {"Action":"DescribeSubnetResponse","DataSet":[{"CreateTime":1562309253,"Gateway":"10.8.0.1","HasNATGW":false,"Name":"DefaultNetwork","Netmask":"16","Remark":"","RouteTableId":"routetable-1bgca25e","Subnet":"10.8.0.0","SubnetId":"subnet-i1yvk2eu","SubnetName":"DefaultNetwork","SubnetType":0,"Tag":"Default","VPCId":"uvnet-ecrfor2j","VPCName":"DefaultVPC","VRouterId":"Default_VRouter","Zone":"hk-01"},{"CreateTime":1562309255,"Gateway":"10.7.0.1","HasNATGW":false,"Name":"DefaultNetwork","Netmask":"16","Remark":"","RouteTableId":"routetable-1bgca25e","Subnet":"10.7.0.0","SubnetId":"subnet-oaglnnnh","SubnetName":"DefaultNetwork","SubnetType":0,"Tag":"Default","VPCId":"uvnet-ecrfor2j","VPCName":"DefaultVPC","VRouterId":"Default_VRouter","Zone":"hk-02"}],"RetCode":0,"TotalCount":4} - 200"
time="2021-05-26T10:31:44+08:00" level=info msg="[78178e47-fa21-488d-a6c3-0af5cfdb3303] do DescribeSubnet successful!"
time="2021-05-26T10:31:44+08:00" level=debug msg="[f8e7152d-10f6-4f94-a238-833f9ed75490] https://api.ucloud.cn?Action=CreateCubePod Action=CreateCubePod&ChargeType=Month&Name=tf-acc-cube-pod-basic&Pod=YXBpVmVyc2lvbjogdjFiZXRhMQpraW5kOiBQb2QKc3BlYzoKICBjb250YWluZXJzOgogICAgLSBuYW1lOiBjdWJlMDEKICAgICAgaW1hZ2U6ICd1aHViLnNlcnZpY2UudWNsb3VkLmNuL2xpYnJhcnkvbmdpbng6MS4xOC4wJwogICAgICByZXNvdXJjZXM6CiAgICAgICAgbGltaXRzOgogICAgICAgICAgbWVtb3J5OiAxMDI0TWkKICAgICAgICAgIGNwdTogMTAwMG0KICAgICAgdm9sdW1lTW91bnRzOiBbXQogIHZvbHVtZXM6IFtdCiAgcmVzdGFydFBvbGljeTogQWx3YXlz&ProjectId=org-xehypo&PublicKey=JInqRnkSY8eAmxKFRxW9kVANYThbzxJKNMEWhQ54N&Quantity=1&Region=hk&Signature=262c111c7568ded916fe8c3593a9d45eac158398&SubnetId=subnet-i1yvk2eu&Tag=tf-acc&VPCId=uvnet-ecrfor2j&Zone=hk-01"
time="2021-05-26T10:31:44+08:00" level=debug msg="[f8e7152d-10f6-4f94-a238-833f9ed75490] {"RetCode":240,"Message":"Permission error [Product]"}\n - 200"
time="2021-05-26T10:31:44+08:00" level=info msg="[f8e7152d-10f6-4f94-a238-833f9ed75490] do CreateCubePod successful!"

Even though UCloud api complained about "Permission error" with RetCode 240, UCloud tf provider output "do CreateCubePod successful!".

The root cause was in ucloud-sdk-go project, let's see CreateCubePodResponse definition:

type CreateCubePodResponse struct {
	response.CommonBase

	// 操作名称
	Action string

	// cube的资源Id
	CubeId string

	// base64编码的yaml
	Pod string

	// 返回码
	 RetCode int
}

CreateCubePodResponse struct embedded a response.CommonBase, whose definition is:

type CommonBase struct {
	Action  string
	RetCode int
	Message string

	requestUUID string

	request request.Common
}

Both structs have field named "RetCode int". When client unmarshal http response in InvokeActionWithPatcher func in client.go:

if err == nil {
		// use patch object to resolve the http response body
		// in general, it will be fix common server error before server bug fix is released.
		body := httpResp.GetBody()

		for _, patch := range patches {
			body = patch.Patch(body)
		}

		err = c.unmarshalHTTPResponse(body, resp)
	}

        // try to get request uuid
	if httpResp != nil {
		requestUUID := httpResp.GetHeaders().Get(headerKeyRequestUUID)
		resp.SetRequestUUID(requestUUID)
	}

	// use response middle to build and convert response when response has been created.
	// such as retry, report traceback, print log and etc.
	for _, handler := range c.responseHandlers {
		resp, err = handler(c, req, resp, err)
	}

The "240" RetCode in http response was actually written into CreateCubePodResponse's RetCode, not it's CommonBase's RetCode. Then when client try to check error from unmarshaled resp with responseHandlers, the handler func's signature is :

type ResponseHandler func(c *Client, req request.Common, resp response.Common, err error) (response.Common, error)

That meant we sent the embedded response.CommonBase with 0 RetCode into response handler and left 240 RetCode outside in CreateCubePodResponse.

After deletion of RetCode in CreateCubePodResponse, now we can see client return err as we expected:

DEBU[2021-05-26T11:19:54+08:00] [9f70eec0-810f-4271-9652-bc404afa8d37] https://api.ucloud.cn?Action=CreateCubePod Action=CreateCubePod&ChargeType=Month&Name=tf-acc-cube-pod-basic&Pod=YXBpVmVyc2lvbjogdjFiZXRhMQpraW5kOiBQb2QKc3BlYzoKICBjb250YWluZXJzOgogICAgLSBuYW1lOiBjdWJlMDEKICAgICAgaW1hZ2U6ICd1aHViLnNlcnZpY2UudWNsb3VkLmNuL2xpYnJhcnkvbmdpbng6MS4xOC4wJwogICAgICByZXNvdXJjZXM6CiAgICAgICAgbGltaXRzOgogICAgICAgICAgbWVtb3J5OiAxMDI0TWkKICAgICAgICAgIGNwdTogMTAwMG0KICAgICAgdm9sdW1lTW91bnRzOiBbXQogIHZvbHVtZXM6IFtdCiAgcmVzdGFydFBvbGljeTogQWx3YXlz&ProjectId=org-xehypo&PublicKey=JInqRnkSY8eAmxKFRxW9kVANYThbzxJKNMEWhQ54N&Quantity=1&Region=hk&Signature=508f708ceaaa7026c083fd0184be3c70fcff56df&SubnetId=subnet-eyqkr1km&Tag=tf-acc&VPCId=uvnet-vjyd5t1n&Zone=hk-01
DEBU[2021-05-26T11:19:54+08:00] [9f70eec0-810f-4271-9652-bc404afa8d37] {"RetCode":240,"Message":"Permission error [Product]"}

Now the problem is, what's the meaning of "Permission error [Product]"?

For the SDK bug, we will release a new version to solve the bug of this redundant field. In the next month, we will also eliminate this problem with duplicate fields.
For the 240 RetCode error, it should be that the current availability_zone has no permission, you can contact technical support to activate relevant permission or change the availability_zone.

Thank you for your explanation @shawnmssu !

We have released a new version v1.28.0 for the SDK bug of this redundant field. Please try again.

Thanks for your feedback, v1.28.0 is released, this PR will be closed.