Repository: sacloud/usacloud (go)

Issue:
VPCルータでの--wireguard指定時にruntime error発生
## 発生している事象

VPCルータの更新時に--wireguardを指定するとruntime errrorが発生する

## usacloudのバージョン

mainブランチのHEAD

## 動作環境


## 表示されたエラーメッセージ

```
$ usacloud vpc-router update wg-quick-test --wireguard test.json
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x105563084]

goroutine 6 [running]:
github.com/sacloud/usacloud/pkg/commands/iaas/vpcrouter.(*routerSettingUpdate).Customize(0x140006b7998, {0x1053c0234?, 0x1059fc0a0?})
	/home/runner/work/usacloud/usacloud/pkg/commands/iaas/vpcrouter/router_setting_update.go:149 +0x974
github.com/sacloud/usacloud/pkg/commands/iaas/vpcrouter.(*updateParameter).Customize(0x0?, {0x105a5ca70?, 0x140004d1500?})
	/home/runner/work/usacloud/usacloud/pkg/commands/iaas/vpcrouter/update.go:84 +0x1ac
github.com/sacloud/usacloud/pkg/core.(*Command).Run(0x1061c5bc0, {0x105a5ca70, 0x140004d1500}, 0x0?, {0x14000287040, 0x1, 0x4})
	/home/runner/work/usacloud/usacloud/pkg/core/command.go:225 +0x74
github.com/sacloud/usacloud/pkg/core.(*Command).CLICommand.func1.1()
	/home/runner/work/usacloud/usacloud/pkg/core/command.go:150 +0x40
created by github.com/sacloud/usacloud/pkg/core.(*Command).CLICommand.func1 in goroutine 1
	/home/runner/work/usacloud/usacloud/pkg/core/command.go:149 +0x1c0
```

## その他、補足事項

その他、補足事項などがあれば、ここに記載してください


Pull request:
Fix: VPCルータ更新時にruntime errorが発生する問題を修正
fixes #1112 

Patch:
diff --git a/pkg/commands/iaas/vpcrouter/router_setting_update.go b/pkg/commands/iaas/vpcrouter/router_setting_update.go
index 91b11d0f..ecf8b8d1 100644
--- a/pkg/commands/iaas/vpcrouter/router_setting_update.go
+++ b/pkg/commands/iaas/vpcrouter/router_setting_update.go
@@ -123,7 +123,7 @@ func (r *routerSettingUpdate) Customize(_ cli.Context) error {
 		if err := util.MarshalJSONFromPathOrContent(r.DNSForwardingData, &df); err != nil {
 			return err
 		}
-		*r.DNSForwarding = df
+		r.DNSForwarding = &df
 	}
 
 	if r.PPTPServerData != "" {
@@ -131,7 +131,7 @@ func (r *routerSettingUpdate) Customize(_ cli.Context) error {
 		if err := util.MarshalJSONFromPathOrContent(r.PPTPServerData, &pptp); err != nil {
 			return err
 		}
-		*r.PPTPServer = pptp
+		r.PPTPServer = &pptp
 	}
 
 	if r.L2TPIPsecServerData != "" {
@@ -139,14 +139,14 @@ func (r *routerSettingUpdate) Customize(_ cli.Context) error {
 		if err := util.MarshalJSONFromPathOrContent(r.L2TPIPsecServerData, &l2tp); err != nil {
 			return err
 		}
-		*r.L2TPIPsecServer = l2tp
+		r.L2TPIPsecServer = &l2tp
 	}
 	if r.WireGuardData != "" {
 		var wireGuard iaas.VPCRouterWireGuard
 		if err := util.MarshalJSONFromPathOrContent(r.WireGuardData, &wireGuard); err != nil {
 			return err
 		}
-		*r.WireGuard = wireGuard
+		r.WireGuard = &wireGuard
 	}
 
 	if r.RemoteAccessUsersData != "" {
@@ -165,7 +165,7 @@ func (r *routerSettingUpdate) Customize(_ cli.Context) error {
 		if err := util.MarshalJSONFromPathOrContent(r.SiteToSiteIPsecVPNData, &s2s); err != nil {
 			return err
 		}
-		*r.SiteToSiteIPsecVPN = s2s
+		r.SiteToSiteIPsecVPN = &s2s
 	}
 
 	if r.StaticRouteData != "" {


Test patch:
diff --git a/pkg/commands/iaas/vpcrouter/router_setting_update_test.go b/pkg/commands/iaas/vpcrouter/router_setting_update_test.go
new file mode 100644
index 00000000..4e1095a5
--- /dev/null
+++ b/pkg/commands/iaas/vpcrouter/router_setting_update_test.go
@@ -0,0 +1,85 @@
+// Copyright 2017-2023 The sacloud/usacloud Authors
+//
+// 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 vpcrouter
+
+import (
+	"reflect"
+	"testing"
+
+	"github.com/sacloud/iaas-api-go"
+)
+
+func Test_routerSettingUpdate_Customize(t *testing.T) {
+	tests := []struct {
+		name     string
+		settings routerSettingUpdate
+		wantErr  bool
+		want     routerSettingUpdate
+	}{
+		{
+			name:     "empty",
+			settings: routerSettingUpdate{},
+			wantErr:  false,
+			want:     routerSettingUpdate{},
+		},
+		{
+			name: "with wireguard json",
+			settings: routerSettingUpdate{
+				WireGuardData: `{"IPAddress":"192.0.2.1/24","Peers":[{"IPAddress":"192.0.2.11","Name":"example","PublicKey":"your-key"}]}`,
+			},
+			wantErr: false,
+			want: routerSettingUpdate{
+				WireGuardData: `{"IPAddress":"192.0.2.1/24","Peers":[{"IPAddress":"192.0.2.11","Name":"example","PublicKey":"your-key"}]}`,
+				WireGuard: &iaas.VPCRouterWireGuard{
+					IPAddress: "192.0.2.1/24",
+					Peers: []*iaas.VPCRouterWireGuardPeer{
+						{
+							Name:      "example",
+							IPAddress: "192.0.2.11",
+							PublicKey: "your-key",
+						},
+					},
+				},
+			},
+		},
+		{
+			name: "with static nat json",
+			settings: routerSettingUpdate{
+				StaticNATData: `[{"GlobalAddress":"192.0.2.1","PrivateAddress":"192.0.2.2"}]`,
+			},
+			wantErr: false,
+			want: routerSettingUpdate{
+				StaticNATData: `[{"GlobalAddress":"192.0.2.1","PrivateAddress":"192.0.2.2"}]`,
+				StaticNAT: &[]*iaas.VPCRouterStaticNAT{
+					{
+						GlobalAddress:  "192.0.2.1",
+						PrivateAddress: "192.0.2.2",
+						Description:    "",
+					},
+				},
+			},
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if err := tt.settings.Customize(nil); (err != nil) != tt.wantErr {
+				t.Errorf("Customize() error = %v, wantErr %v", err, tt.wantErr)
+			}
+			if !reflect.DeepEqual(tt.settings, tt.want) {
+				t.Errorf("unexpected router setting state: got = %v, actual = %v", tt.settings, tt.want)
+			}
+		})
+	}
+}